Domanda

Ho un controllo utente web con un UpdatePanel. Sul mio mainpage ho avuto come 3 di tali controlli. Ora voglio avere un timer nella mainpage che farebbe scattare i UpdatePanels nei controlli utente web.

Come posso gestire questo?

Grazie in anticipo.

È stato utile?

Soluzione

Uso del timer di controllo AJAX UpdatePanel come trigger

Implementare un aggiornamento-funzione nel vostro UserControl che richiama l'aggiornamento-Function dei loro update-Panels e chiamarlo dal Mainpage nel TimerTick-evento per ogni controllo. Impostare l'UpdateMode di UpdatePanels = dei vostri usercontrols condizionale.

Per esempio nella tua Codebehind di UserControl:

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

E nella vostra pagina principale:

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 ascx file del UserControl:

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

In aspx file della pagina principale:

<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>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top