c# - RadGrid Countdown Clock in Template Column -
i have telerik radgrid bound sqldatasource (items databound), in 1 of data bound columns there value of type datetime
format h:mm:ss {0:h:mm:ss}
. added template column shows "time left"
so: start time - = time left
i wanted ajaxified added timer , label inside updatepanel in template column - every tick (1000ms) recalculate time left
.
this seems work first row in radgrid
here code snippets
asp
<telerik:gridtemplatecolumn filtercontrolalttext="filter templatecolumn column" uniquename="templatecolumn" headertext="starting in"> <itemtemplate> <contenttemplate> <asp:updatepanel id="updatepanel1" runat="server"> <contenttemplate> <asp:label id="lbltime" runat="server" text="00:00:00"></asp:label> <asp:timer id="timer1" runat="server" interval="1000" ontick="timer1_tick"></asp:timer> </contenttemplate> </asp:updatepanel> </contenttemplate> </itemtemplate> </telerik:gridtemplatecolumn>
c#
protected void timer1_tick(object sender, eventargs e) { griddataitem viewitem = (sender system.web.ui.timer).parent.parent.parent.parent griddataitem; datetime starttime = datetime.parseexact((viewitem.findcontrol("strat_timelabel") label).text, "h:mm:ss", system.globalization.cultureinfo.currentculture); datetime nowtime = system.datetime.now; timespan ts = new timespan(); ts = starttime - nowtime; griddataitem item = (sender system.web.ui.timer).parent.parent.parent.parent griddataitem; if (ts.seconds >= 0 && ts.minutes > 0) { datetime d = new datetime(2000, 01, 01, ts.hours, ts.minutes, ts.seconds); (item.findcontrol("lbltime") label).text = d.tostring("h:mm:ss"); } else { (item.findcontrol("lbltime") label).text = "00:00:00"; } }
any or advice appreciated
if interested found work around.
i moved updatepanel encapsulate entire radgrid (the timer still contained in updatepanel)
i changed c# code behind of timer1_tick to
foreach (griddataitem item in radgrid1.items) { datetime starttime = datetime.parseexact((item.findcontrol("strat_timelabel") label).text, "h:mm:ss", system.globalization.cultureinfo.currentculture); datetime nowtime = system.datetime.now; timespan ts = new timespan(); ts = starttime - nowtime; system.web.ui.timer).parent.parent.parent.parent griddataitem; if (ts.seconds >= 0 && ts.minutes > 0) { datetime d = new datetime(2000, 01, 01, ts.hours, ts.minutes, ts.seconds); (item.findcontrol("lbltime") label).text = d.tostring("h:mm:ss"); } else { (item.findcontrol("lbltime") label).text = "complete"; } }
then every gridcommand disabled/enable timer
insertcommand: timer1.enabled = false; editcommand: timer1.enabled = false; updatecommand: timer1.enabled = true; cancelcommand: timer1.enabled = false;
probably not eloquent way have liked contain postback event in item template... works... improvements , suggestions welcome.
Comments
Post a Comment