I have two ASP.NET AJAX UpdatePanels . there are two timers with diffrent intervals. is it possible to u开发者_开发技巧pdate two updatepanels simultaneously together ? like multi thread application . each should be UpdatePanel Update in separate thread in one time. i wrote this code but second timer does not work :
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Welcome to ASP.NET!
</h2>
<p>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</p>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="2000">
</asp:Timer>
<asp:Timer ID="Timer2" runat="server" Interval="2000">
</asp:Timer>
</asp:Content>
Behind Code :
Protected Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
Label1.Text = Date.Now
End Sub
Protected Sub Timer2_Tick(sender As Object, e As System.EventArgs) Handles Timer2.Tick
Label2.Text = Date.Now
End Sub
Actually, your UpdatePanels are useless in this example, because your Timers are outside of them. As a result, every time your event gets triggered, it does a full page refresh. (This is in part why you never see the second Timer get hit -- by the time the first timer gets hit, the entire page gets refreshed, so the counter resets on the first timer)
So, you need to accomplish two things to fix your page:
- Get the UpdatePanels to work properly with the Timers so that only asynchronous postbacks are occurring.
- Make sure that each Timer only causes one UpdatePanel to refresh.
The first can be taken care of by either moving the Timer into the UpdatePanel, as a child, or by using the <asp:Triggers>
element to basically say "The only thing that will update my UpdatePanel is this timer."
The second can be taken care of by setting the UpdateMode=Conditional
attribute on each UpdatePanel.
Try this:
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Welcome to ASP.NET!
</h2>
<p>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</p>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer2" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="2000" ontick="Timer1_Tick">
</asp:Timer>
<asp:Timer ID="Timer2" runat="server" Interval="2000" ontick="Timer2_Tick">
</asp:Timer>
</asp:Content>
I have to run to work now, so bear with me if you have any questions ;-)
精彩评论