1

我有一个带有更新面板的网络用户控件。在我的主页上,我得到了 3 个这样的控件。现在我想在主页中有一个计时器,它会触发 Web 用户控件中的更新面板。

我该如何管理?

提前致谢。

4

1 回答 1

1

将 AJAX 计时器控件用作 UpdatePanel 触发器

在您的 UserControl 中实现一个 Update-Function,它调用其更新面板的 Update-Function,并从 TimerTick-Event 的 Mainpage 中为每个控件调用它。设置 UserControls 的 UpdatePanels=Conditional 的 UpdateMode。

例如在您的 UserControl 的代码隐藏中:

Public Sub Update()
    'bind Data to your UpdatePanel's content f.e.:
    Me.Label1.Text = Date.Now.ToLongTimeString
    Me.UpdatePanel1.Update()
End Sub

在您的主页中:

Private myControls As New List(Of WebUserControl1)

Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
     For i As Int32 = 1 To 10
        Dim newControl As WebUserControl1= DirectCast(LoadControl("./WebUserControl1.ascx"), WebUserControl1)
        myControls.Add(newControl)
        MainPanel.Controls.Add(newControl)
     Next
End Sub

Protected Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    'in this example added dynamically
    For Each ctrl As WebUserControl1 In Me.myControls 
        ctrl.Update()
    Next
End Sub

在 UserControl 的 ascx 文件中:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>             
    </ContentTemplate>
</asp:UpdatePanel>

在主页的 aspx 文件中:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
     <asp:Panel ID="MainPanel"  runat="server">
        <asp:Timer ID="Timer1" runat="server" Interval="1000"></asp:Timer>
     </asp:Panel>             
    </ContentTemplate>
</asp:UpdatePanel>
于 2010-05-10T13:50:55.960 回答