c# - Late timer label display while page loading using update panel -
i have used c# timer updatepanel in webpage, working except during page loads timer label displays after 3 seconds ie: 3:00 mins timer displays 2:57 secs .
so how resolved. in advance....
my aspx code is:
<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" font-bold="true" font-names="arial" font-size="x-large" forecolor="#6600cc"></asp:label> </contenttemplate> <triggers> <asp:asyncpostbacktrigger controlid="timer1" eventname ="tick" /> </triggers> </asp:updatepanel> </div>
my asp.cs code is:
if (0 > datetime.compare(datetime.now, datetime.parse(session["timeout"].tostring()))) { string seconds = ((int32)datetime.parse(session["timeout"].tostring()).subtract(datetime.now).seconds).tostring(); if (convert.toint32(seconds) < 10) { lbltimer.text = string.format("time left: 00:0{00}:0{01}", ((int32)datetime.parse(session["timeout"].tostring()).subtract(datetime.now).totalminutes).tostring(), ((int32)datetime.parse(session["timeout"].tostring()).subtract(datetime.now).seconds).tostring()); } else { lbltimer.text = string.format("time left: 00:0{00}:{01}", ((int32)datetime.parse(session["timeout"].tostring()).subtract(datetime.now).totalminutes).tostring(), ((int32)datetime.parse(session["timeout"].tostring()).subtract(datetime.now).seconds).tostring()); } }
ok
i guess setting session
variable somewhere @ start of page, possible in page_load
. after that, time needed construct page, or execute database statements. after executes timer code , time further.
you should know using timer
, of sort in .net isn't 100% reliable. thing timer suspends background, , processor other things. after @ least interval time (so little longer), timer resumes , continues execute. means have delay in code.
that means possible have miscalculations based on timer assumptions too. possibly first ping-back of timer happened , due calculation rounding, off 2+ seconds already.
as side note: parse integer once, make code easier read. also, due repeated calculation, time within statement may differ.
Comments
Post a Comment