Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
I want to create a online exam aaplication in asp.net.In that case I want to create countdown timer on my first page which start by button click(without post back).The problem is when I click on button the page load event fire and timer not work anymore.
today I was successfully created a page but the timer start with page load,it's not right way.
Please help me with complete code because I am new in this programming field.
Please give me also second page code for start the timer with previous time calculation.
Urgent help me............
Posted
Comments
Adrishya 10-Oct-13 2:41am    
You can use ajax for the purpose.
Jyoti Pandey 10-Oct-13 2:48am    
I know ajax but I want complete code for my question please help me.....
Siva Hyderabad 10-Oct-13 2:49am    
i send link...i hope ,it is useful for you...try this.all the best
Siva Hyderabad 10-Oct-13 2:51am    
it shows,your exam started time,and also which time u have..i already use this.
Jyoti Pandey 10-Oct-13 3:21am    
I use your updated code and that shows error on session variable in page load event.
help me..

 
Share this answer
 
source code......
XML
<center>
<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>
</center>


c# code.....

write this in page load....
VB
if (!SM1.IsInAsyncPostBack)

                Session["timeout"] = DateTime.Now.AddMinutes(30).ToString();



for timer click...
C#
protected void timer1_tick(object sender, EventArgs e)
       {
           if (0 > DateTime.Compare(DateTime.Now, DateTime.Parse(Session["timeout"].ToString())))
           {
               lblTimer.Text = string.Format("Time Left: 00:{0}:{1}", ((Int32)DateTime.Parse(Session["timeout"].ToString()).Subtract(DateTime.Now).TotalMinutes).ToString(), ((Int32)DateTime.Parse(Session["timeout"].ToString()).Subtract(DateTime.Now).Seconds).ToString());
           }
           else
           {
               timer1.Enabled = true;
               Response.Redirect("Logout.aspx");

           }
       }



try this,it will work.............
 
Share this answer
 
Comments
Jyoti Pandey 11-Oct-13 1:01am    
This code is for only one page...I am applying this on my application and it is beneficial for me. But the problem is how can send remain time into another page by session variable..!!
Please Help Me..!!
Online Examination System 3-May-15 8:42am    
Thank you for this awesome code... . but i wants to know to that how to fire a event when the given time has been completed ..!!
Member 11197134 2-Jun-16 15:04pm    
did you got the answer about how to fire an event after the timer is completed?
Member 10774811 12-Jul-15 12:03pm    
thanks , very helpful code
Member 11629954 21-Nov-15 11:46am    
It behaves unexpected on the web server it does not decrements as per the interval given please help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900