ASP.NET/AJAX- Web用户控制更新面板从外部触发
-
03-10-2019 - |
题
我有一个带有UpdatePanel的Web用户控件。在我的主页上,我得到了其中的3个控件。现在,我想在主页中有一个计时器,该计时器将触发Web用户控件中的UpdatePanels。
我该如何管理?
提前致谢。
解决方案
在您的UserControl中实现更新功能,该功能称为其更新面板的更新功能,并从Timertick-Event中的主页调用每个控件的主页。设置USERCONTROLS UPDATEPANELS =条件的UpdateMode。
例如,在您的usercontrol的CodeBehind中:
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>
不隶属于 StackOverflow