ASP.NET/AJAX - Web user control update panel triggering from outside
I've got an web user control with an updatepanel. On my mainpage I got like 3 of those controls. Now I want to have an timer in the mainpage which would trigger the updatepanel开发者_StackOverflow社区s in the web user controls.
How can I manage this?
Thanks in advance.
Using the AJAX Timer Control as an UpdatePanel Trigger
Implement an Update-Function in your UserControl which calls the Update-Function of their Update-Panels and call it from the Mainpage in the TimerTick-Event for every control. Set the UpdateMode of your UserControls's UpdatePanels=Conditional.
For example in your UserControl's Codebehind:
Public Sub Update()
'bind Data to your UpdatePanel's content f.e.:
Me.Label1.Text = Date.Now.ToLongTimeString
Me.UpdatePanel1.Update()
End Sub
And in your Mainpage:
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
In the UserControl's ascx-file:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
In the Mainpage's aspx-file:
<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>
精彩评论