Saturday, March 10, 2012

Use links to stop and start timer trigger

Hi,

I don't know if I've correctly understood you requirements. Anyways, here is an example with an UpdatePanel and two LinkButtons:

<%@. Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> protected void Page_Load(object sender, EventArgs e) { if (ScriptManager.GetCurrent(this).IsInAsyncPostBack) { Label1.Text = DateTime.Now.ToString(); } } protected void LinkButton1_Click(object sender, EventArgs e) { Timer1.Enabled = true; } protected void LinkButton2_Click(object sender, EventArgs e) { Timer1.Enabled = false; }</script><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> </div> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <Triggers> <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" /> </Triggers> <ContentTemplate> <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Start</asp:LinkButton> <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">Stop</asp:LinkButton> <asp:Label ID="Label1" runat="server"></asp:Label> </ContentTemplate> </asp:UpdatePanel> <asp:Timer ID="Timer1" runat="server" Interval="5000" Enabled="False"> </asp:Timer> </form></body></html>

Hi Garbin, thanks for your answeer, but it's not the right option to my project because I'm using just one timer to control two UpdatePanels, and I want to stop only one of these UpdatePanels from being controlled by the Timer control.

Hi,

did you try to programmatically add and remove the trigger from the UpdatePanel?


That is the problem. how to do this? I think the best way or maybe the only way to do this is to control this actions by using client script, but controling Ajax functions in Client Script is a little bit complicated.

Hi,

I've played a bit with it and actually you don't need to add a trigger for the second UpdatePanel. All you need is a flag (EnablePolling in the example) that when set to true causes the second UpdatePanel to be updated in the Tick event handler:

<%@. Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> private bool EnablePolling { get { object ret = ViewState["EnablePolling"]; return (ret != null) ? (bool)ret : false; } set { ViewState["EnablePolling"] = value; } } protected void LinkButton1_Click(object sender, EventArgs e) { EnablePolling = true; } protected void LinkButton2_Click(object sender, EventArgs e) { EnablePolling = false; } protected void Timer1_Tick(object sender, EventArgs e) { Label1.Text = Label2.Text = DateTime.Now.ToString(); if (EnablePolling) { UpdatePanel2.Update(); } }</script><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" /> <asp:Timer ID="Timer1" runat="server" Interval="5000" OnTick="Timer1_Tick" /> <div> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <Triggers> <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" /> </Triggers> <ContentTemplate> <asp:Label ID="Label1" runat="server"></asp:Label> <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Start Panel 2 Polling</asp:LinkButton> <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">Stop Panel 2 Polling</asp:LinkButton> </ContentTemplate> </asp:UpdatePanel> </div> <div> <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:Label ID="Label2" runat="server"></asp:Label> </ContentTemplate> </asp:UpdatePanel> </div> </form></body></html>

Garbin, can I do this using Client Script? Because the links that will be controlling the start and stop flag is not running on the server, they already run a JavaScript function to change the visible Tab (I've created a kind of TabScript using JavaScript and CSS, and the links that will be controlling the start and stop are the caption of each Tab).

Thanks.


Hi,

using client script you could enable/disable the timer but you cannot add/remove a trigger.


The last one...

How can I enable/disable the timer using Client Script so?


Hi,

if I'm not mistaken, the Sys._Timer instance has _startTimer and _stopTimer methods. By the way, if you stop the timer you also prevent UpdatePanel1 from updating. Is this part of your requirements?


I want to prevent the UpdatePanel from being controlled by the timer (during some time) and update it just when I call a method from one of the controls within the updatePanel.

Can I do this?

No comments:

Post a Comment