2010-05-05 73 views

回答

1

Using the AJAX Timer Control as an UpdatePanel Trigger

實現你的用戶控件這就要求他們更新-面板的更新,功能和從TimerTick-事件的網主頁的每個控制稱它爲更新-功能。 設置UserControls的UpdatePanels = Conditional的UpdateMode。

例如,在你的用戶控件的代碼隱藏:

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 

在用戶控件的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>