无法动态加载标签

时间:2010-01-13 18:21:09

标签: .net asp.net vb.net

我正在尝试动态加载标签(使用AjaxToolKit)。首先,当我在页面加载时点击button1时,它会加载tab1(效果很好),当我点击button2时,它会加载tab2

所有这些标签都是ascx页面。

tab1我正在加载WebUserControl.ascx,这可行。

的问题:

1)当您运行代码时,首先点击button2,它不会动态加载tab2动态,但是当您第一次点击button1时它会动态。 2)重复点击button1button2应该一次又一次地加载相同的标签,但事实并非如此。 3)第三,主要问题是我试图在loadtab2加载另一个.ascx,其中应包含webparts。我应该能够动态地将另一个自定义用户控件加载到这些Web部件中。但我无法这样做。

任何人都可以帮助解决这些问题吗?

这是我的代码:

Default.aspx的

<div>    
    <asp:AjaxScriptManager ID="AjaxScriptManager1" runat="server">
    </asp:AjaxScriptManager>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
        Text="Show Tab1" />
    <asp:Button ID="Button2" runat="server" onclick="Button2_Click"
        Text="Show Tab2" />
    <asp:Button ID="Button3" runat="server" Text="Button" />   
</div>
<p>
  <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</p>  

Default.aspx.vb

Imports System
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Collections.Generic
Imports System.Web.UI.WebControls
Imports System.Collections
Imports System.Collections.Specialized
Imports AjaxControlToolkit
Imports AjaxControlToolkit.ToolkitScriptManager
Public Class _Default
    Inherits System.Web.UI.Page
    Dim tc1 As New TabContainer()
    Dim uc1 As Control
    Dim tp1 As New TabPanel()
    Dim tp2 As New TabPanel()
    Dim tp3 As New TabPanel()
    Dim Wp1 As New WebPartManager()

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Page.PreRender += new EventHandler(Page_PreRender);
        If Session("Tab1") <> Nothing Then
            If Session("Tab1").ToString() = "true" Then
                LoadTab1()
            End If
        ElseIf Session("Tab2") <> Nothing Then
            If Session("Tab2").ToString() = "true" Then
                LoadTab2()
            End If
        End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

        If Session("Tab1") Is Nothing Then
            Session("Tab1") = "true"
            LoadTab1()
        ElseIf Session("Tab1").ToString() <> "true" Then
            LoadTab1()
        End If
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        If Session("Tab2") Is Nothing Then
            Session("Tab2") = "true"
            LoadTab2()
        ElseIf Session("Tab2").ToString() <> "true" Then
            LoadTab2()
        End If
    End Sub
    Private Sub LoadTab1()
        uc1 = LoadControl("WebUserControl.ascx")
        tp1.HeaderText = "Tab1"
        'tp2.HeaderText = "Tab2"

        tp1.Controls.Add(uc1)
        tc1.Tabs.Add(tp1)
        'tc1.Tabs.Add(tp2)


        If Session("Tab2") <> Nothing Then
            LoadTab2()
        End If

        'if (Session["Tab2"] == null)
        '{
        PlaceHolder1.Controls.Add(tc1)
        '}
    End Sub

    Public Sub LoadTab2()

        Dim uc2 As UserControl = CType(LoadControl("WebUserControl2.ascx"), UserControl)
        uc2.ID = "control"

        tp2.HeaderText = "Tab2"
        tc1.Tabs.Add(tp2)

        tp2.controls.add(uc2)

(Error:A Zone can only be added to the Page in or before the Page_Init event...)

End Sub

WebUserControl.ascx.vb

Protected Sub Page_Init()
    Dim Btn As New Button()
    Btn.ID = "TestButton"

    Dim zone1 As New WebPartZone()
    zone1.ID = "zone1"
    Panel1.Controls.Add(zone1)
    ' WebPart myWebPart = WebPartManager1.CreateWebPart(Btn);
    'myWebPart.ID = "2";
    'myWebPart.Title = "MyWebPart";
    Dim uc As Control = Me.LoadControl("FeaturedControl.ascx")
    uc.ID = "control"
    Dim myWebPart As GenericWebPart = WebpartManager1.CreateWebPart(uc)
    WebpartManager1.AddWebPart(myWebPart, zone1, 1)
    'WebPartManager1.AddWebPart(controol, zone1, 1);
End Sub

1 个答案:

答案 0 :(得分:0)

尝试将代码从Page_Load()事件移出到Page_Init()事件

相关问题