1
Reply

How to update label on tick event in web form

Sajid Hussain

Sajid Hussain

Mar 7 2016 5:32 AM
321


down votefavorite

I have a web form in which i want to show timing label for user,like 180 second, i have done using following code,

<div> <asp:ScriptManager ID="SM1" runat="server"></asp:ScriptManager> <asp:Timer ID="timer1" runat="server" Interval="1000" OnTick="timer1_tick"> </asp:Timer> </div> <div> <asp:UpdatePanel ID="updPnl" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:Label  ID="lblTimer" runat="server"></asp:Label> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="timer1" EventName="tick" /> </Triggers> </asp:UpdatePanel> </div>

and code behind

protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Page.Header.Title = "Test"; } if (!SM1.IsInAsyncPostBack) Session["TimeCount"] = DateTime.Now.AddSeconds(180).ToString(); }

and

 protected void timer1_tick(object sender, EventArgs e) { if (0 > DateTime.Compare(DateTime.Now, DateTime.Parse(Session["TimeCount"].ToString()))) {                 lblTimer.Text = "Number of Second Left: " + ((Int32)DateTime.Parse(Session["TimeCount"]. ToString()).Subtract(DateTime.Now).TotalSeconds).ToString(); } else { } } 

problem i am facing that each time user click on button in page counter refresh to 180 again,Secondly this show fix value like 177,170,i want to show that label auto decreasing ?

c#-4.0

Answers (1)