i have a master page which uses script manager and on another page i want to show a digital clock which counts from 0 to 10 minutes.
asp.net code:
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
</asp:ScriptManagerProxy>
<asp:Timer ID="Timer1" runat="server" Interval="5000" ontick="Timer1_Tick">
</asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
C# code:
int min=0,sec=0;
protected void Timer1_Tick(object sender, EventArgs e)
{
if (sec == 55)
{
min = min + 1;
sec = 0;
}
else
{
sec = sec + 5;
}
Label1.Text = min.ToString() +":"+ sec.ToString();
if(min==10&&sec==0)
{
Timer1.Enabled=false;
Label1.Text = "clock over!";
}
the page starts with 0:5 and doesn’t change at all. pls point out if there is an error in d code!
My bet is that it will be your update panel. For debug purposes
In fact point 2 could be done anyway – I’m not sure of the benefit of having it as a postback trigger. It would do just as well with the timer inside
However
Honestly I wouldn’t do this. Won’t the Tick method perform a postback (albeit partial) just to get the timer display going.
There are lots of freely available components that will run purely on the client side which will be a lot (lot) more efficient e.g.
JQuery Timer
EDIT
Actually I think I’ve spotted the problem. It is how you record mins and secs. These are in variables and would be reset every postback (Timer tick). Youu would need to store them into something that would perist postback e.g. ViewState
But homestly – still don’t do it. Use the JQuery component or similar