ASP.Net Code
While displaying a count down timer on your Web Page you need to do following things :
On the web page you need to add 7 controls.
In that one is HTML Text box which displays the count down time
<input type=”text” readonly =”true” size=”8″ id=”counter” style=”text-align:center; font-size:x-large; font-weight:bold; color:Red;” />
and rest of six are hidden fields in which three of them are HTML controls and other are ASP controls looks loke this
<input type=”hidden” size=”8″ id=”HidHours” value=”<%=HidH.Value%>“/>
<input type=”hidden” size=”8″ id=”HidMinutes” value=”<%=HidM.Value%>“/>
<input type=”hidden” size=”8″ id=”HidSeconds” value=”<%=HidS.Value%>“/>
<asp:HiddenField ID=”HidH” runat=”server” Value=”0″ />
<asp:HiddenField ID=”HidM” runat=”server” Value=”0″ />
<asp:HiddenField ID=”HidS” runat=”server” Value=”0″ />
Now you need to write the main JavaScript part which displays this count down time
<script>
var hours=document.getElementById(‘HidHours’).value;
var minutes=document.getElementById(‘HidMinutes’).value;
var seconds=document.getElementById(‘HidSeconds’).value;
function
display()
{
if (seconds<=0)
{
if ((hours==0)&&(minutes==0))
seconds=0;
else
{
seconds=60;
minutes-=1;
}
}
if (minutes<=0)
{
if ((hours<0)&&(minutes<0))
{
hours = minutes = seconds = 0;
}
else
{
if ((hours==0)&&(minutes==0))
hours=minutes=0;
if ((hours>0)&&(minutes<0))
{
minutes=59;
hours-=1;
}
}
}
if ((minutes<=-1)||(hours<=-1))
{
if (hours<=-1)
{
minutes=0;
hours+=1;
}
else
minutes-=1;
seconds=0;
minutes+=1;
}
else
if (seconds>0)
seconds-=1;
document.getElementById(‘counter’).value=hours+“:”+minutes+“:”+seconds;
setTimeout(“display()”,1000);
}
display();
</script>
The Main Concept Over Here is the Time from where the count down begins that time you need to pass into ASP Hidden fields whose id are HidH, HidM and HidS which defines hours, minutes and seconds respectively. Now these values are passed to HTML Hidden Controls whose id are HidHours, HidMinutes and HidSeconds since the values of ASP controls value’s can not be passed directly to JavaScript code. We kept HTML controls as bridge between ASP controls Value’s and JavaScript. and Now Javascript function display() is called recursively until hours, minutes and seconds becomes zero. Once it is zero you can redirect to any page you want
C# Code
TimeSpan ts;
HidH.Value = Convert.ToString(ts.Hours);
HidM.Value = Convert.ToString(ts.Minutes);
HidS.Value = Convert.ToString(ts.Seconds);
click here : http://groups.google.co.in/group/dotnethelp2008/web/count-down-timer-asp-net-javascript-and-c?hl=en&msg=ns
Hi friend,
This is srini, when i was in need of timer in ma web page i find this coding, this works really fine, but when i try to raise a popup using javascript, the timer stops and when the popup window closes it continues…. This should not happen according to my page so is there is any other way to sort out this issue? If so pls do contact me through my mail…. I think both are script side coding thats is why the timer stops
Excellent post.
thank you for u idea and code
Mr. Ganesh
i am creating a web application and i want to use countdown timer in that.
i seen your code but i have some problem with this code.
when i run my web application a text box showing a value like that
01:30:59
after each second the value of text box like that
01:30:58
after that
01:30:59
after that
01:30:58
my qus is that
why text box shows the value of sec only 58 and 59
How Come? It works for me correctly I used it many times… Can u please check once again? may be you are missing some thing?
this is my code in default.aspx
plzzz help me ……
Untitled Page
<input type="hidden" id="HidHours" value="” />
<input type="hidden" id="HidMinutes" value="” />
<input type="hidden" id="HidSeconds" value="” />
var hours=document.getElementById(‘HidHours’).value;
var minutes=document.getElementById(‘HidMinutes’).value;
var seconds=document.getElementById(‘HidSeconds’).value;
function
display()
{
if (seconds<=0)
{
if ((hours==0)&&(minutes==0))
seconds=0;
else
{
seconds=60;
minutes-=1;
}
}
if (minutes<=0)
{
if ((hours<0)&&(minutes0)&&(minutes<0))
{
minutes=59;
hours-=1;
}
}
}
if ((minutes<=-1)||(hours<=-1))
{
if (hours0)
seconds-=1;
if (seconds <= 9 && seconds.toString().length < 2)
seconds = "0"+seconds;
if (minutes <= 9 && minutes.toString().length < 2)
minutes = "0"+minutes;
if (hours <= 9 && hours.toString().length < 2)
hours = "0"+hours;
document.getElementById('counter').value=hours+":"+minutes+":"+seconds;
setTimeout("display()",1000);
}
display();
and default.aspx.cs code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
TimeSpan ts = new TimeSpan(1,30,59);
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Timer1_Tick(object sender, EventArgs e)
{
HidH.Value = Convert.ToString(ts.Hours);
HidM.Value=Convert.ToString(ts.Minutes);
HidS.Value = Convert.ToString(ts.Seconds);
}
}
plzz see it and suggest me where i missed……..
sorry ignore the above code
this is my code in default.aspx
Untitled Page
<input type="hidden" id="HidHours" value="” />
<input type="hidden" id="HidMinutes" value="” />
<input type="hidden" id="HidSeconds" value="” />
var hours=document.getElementById(‘HidHours’).value;
var minutes=document.getElementById(‘HidMinutes’).value;
var seconds=document.getElementById(‘HidSeconds’).value;
function
display()
{
if (seconds<=0)
{
if ((hours==0)&&(minutes==0))
seconds=0;
else
{
seconds=60;
minutes-=1;
}
}
if (minutes<=0)
{
if ((hours<0)&&(minutes0)&&(minutes<0))
{
minutes=59;
hours-=1;
}
}
}
if ((minutes<=-1)||(hours<=-1))
{
if (hours0)
seconds-=1;
if (seconds <= 9 && seconds.toString().length < 2)
seconds = "0"+seconds;
if (minutes <= 9 && minutes.toString().length < 2)
minutes = "0"+minutes;
if (hours <= 9 && hours.toString().length < 2)
hours = "0"+hours;
document.getElementById('counter').value=hours+":"+minutes+":"+seconds;
setTimeout("display()",1000);
}
display();
mr . ganesh i can’t pesting here complete code i don’t knw whats d problem in pesting
i email you the complete code
plzz give ur email id……..
I want this code online exam time period of 20 mins total exam how can i write in javascript
Untitled Page
<input type="hidden" id="HidHours" value="” />
<input type="hidden" id="HidMinutes" value="” />
<input type="hidden" id="HidSeconds" value="” />
Hi Ganesh,
Your script is working fine, thanks for pasting script here.
thank you very much.
hello ganesh,
i were use ur above code for begins countdown at quize application but its not working..i mean no any output shows like time count down, here just display only empty input textbox. ..i have used code in such a way….
……………..default.aspx code……………
Untitled Page
var hours=document.getElementById(‘HidHours’).value;
var minutes=document.getElementById(‘HidMinutes’).value;
var seconds=document.getElementById(‘HidSeconds’).value;
function
display()
{
if (seconds<=0)
{
if ((hours==0)&&(minutes==0))
seconds=0;
else
{
seconds=60;
minutes-=1;
}
}
if (minutes<=0)
{
if ((hours<0)&&(minutes0)&&(minutes<0))
{
minutes=59;
hours-=1;
}
}
}
if ((minutes<=-1)||(hours<=-1))
{
if (hours0)
seconds-=1;
if (seconds <= 9 && seconds.toString().length < 2)
seconds = "0"+seconds;
if (minutes <= 9 && minutes.toString().length < 2)
minutes = "0"+minutes;
if (hours <= 9 && hours.toString().length < 2)
hours = "0"+hours;
document.getElementById('counter').value=hours+":"+minutes+":"+seconds;
setTimeout("display()",1000);
}
display();
………….default.aspx.cs code………………………………………..
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
TimeSpan ts = new TimeSpan(1, 30, 59);
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Timer1_Tick(object sender, EventArgs e)
{
TimeSpan ts = new TimeSpan(1, 30, 59);
HidH.Value = Convert.ToString(ts.Hours);
HidM.Value = Convert.ToString(ts.Minutes);
HidS.Value = Convert.ToString(ts.Seconds);
}
}
is it neccessary take Timer1_Tick….?
hello ganesh
how r u?
i have some problem in creating my online examination application.
problem is that, how can i pass multiple values randomly in radiobuttonlist from DB….and if any one item of radiobutton is null than it shud not be visible…
i was try on that but it cudnt. HERE IS CODE THAT IM USING..
if (incQuestion 0)
{
DataRow dtr;
int i = 0;
while (i < ds.Tables[0].Rows.Count)
{
dtr = ds.Tables[0].Rows[i];
Session["Answer"] = Convert.ToString(Convert.ToInt32(dtr["Correct"].ToString()) – 1);
lblQuestion.Text = "Q." + incQuestion + " " + dtr["Question"].ToString();
txtAnswerHd.Text = dtr["Answer"].ToString();
rblOptions.ClearSelection();
rblOptions.Items.Clear();
rblOptions.Items.Add(dtr["Option1"].ToString());
rblOptions.Items.Add(dtr["Option2"].ToString());
rblOptions.Items.Add(dtr["Option3"].ToString());
rblOptions.Items.Add(dtr["Option4"].ToString());
if (rblOptions.Items != null)
{
rblOptions.Items.Add(dtr["Option5"].ToString());
}
else
{
rblOptions.Visible = false;
}
if (rblOptions.Items != null)
{
rblOptions.Items.Add(dtr["Option6"].ToString());
}
else
{
rblOptions.Visible = false;
}
i++;
}
incQuestion += 1;
}
}
plzz help me..
and if u were work on crystal report than. i have to discuss on dynamically generate CrystalReport also.
thanks .
Regards
PRADEEP KODLEY
how to run this javascirpt?
i copy paste javascript and added controls in asp.net body/form tag.
doesn’t display anything?
your code is working very fine.
but i want to redirect to another page when timer stops…
i tried many times but i could not
plz help me!!!!!
Hi
I am using this script for showing count down timer in my asp.net application but instead of showing time it shows undefine:undefine:undefine.
Can you tell me Why it is haapened .
Thanks
Warm Regards
Mayank Gaur
Hi
I am using this script for showing count down timer in my asp.net application but instead of showing time it shows undefined:undefined:undefined.
Can you tell me Why it is haapening .
Thanks
Warm Regards
Mayank Gaur
Hi Ganesh,
How to use the timer in windows application?
hi,
i tried this sample but in text box nothing is displaying empty text box is coming please guide me for that.
Thanks,
udaya
hi ganesh . . ur code works very fine . . the thing is i have connected an formview to table where the qusetions wil display in form view . . each time when i click next or prev the timer start running from start . . it should not happen. once the user start answering until he complete answering all question the timer should not start from beginning . . What should i do for that . . and where i have place the code . .can u please answer me . .
Regards,
bala
Thanks in advance.