Click here to Skip to main content
13,400,969 members (37,723 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 13 Sep 2010

Online Exam in C# ASP NET

, 13 Sep 2010
Rate this:
Please Sign up or sign in to vote.
Online Examination Project with CountDown Timer


Most of us have come across Examination / Quiz projects. The important aspects of those projects is to have a timer that runs through the entire duration of the exam, having the timer running without reset by postbacks is the main concern of this article.


The idea is to keep the value of the timer in view state and to supply the same on postback so that the timer will keep running from its previous value.

Using the Script

Understanding the script used here is not a big deal I guess, and am leaving it with a little explanation, go through the attachment for better understanding,

Timer is initiated in the page PreRender event, supplying the StartValue, Timerinterval and the clientId of the output element.

When the timer is started for the first timer, it is supplied with the duration of the examination say it is 30 min, and assume that the candidate spent 2 mins on a question and the postback occurs on 28 min, this value is kept in the view state and the same is used to start the timer again on the page PreRender event:

function myTimer(startVal,interval,outputId, dataField)
this.value = startVal;
this.OutputCntrl = document.getElementById(outputId);
this.currentTimeOut = null;
this.interval = interval;

myTimer.prototype.Seconds = function(value){
var hoursMillSecs = (this.Hours(value)*3600000)
var minutesMillSecs = (this.Minutes(value)*60000)
var total = (hoursMillSecs + minutesMillSecs)
var ans = Math.floor(((this.value - total)%60000)/1000);

this.OutputCntrl.innerHTML = this.Hours(current) + ':' + this.Minutes(current) 
+ ':' +  this.Seconds(current);

Keeping the Value in ViewState

You have to register the scrip with RegisterStartupScript in the codebehind file that supplies the view state value to the script and keeps the timer running.

Timer is initiated in PreRender event and the start value of the timer is requested from the view state and supplied to the timer, and this value changes on each postback as the exam is on progress.

void Page_PreRender(object sender, EventArgs e)
      StringBuilder bldr = new StringBuilder();
      bldr.AppendFormat("var Timer = new myTimer({0},{1},'{2}','timerData');",
          this.timerStartValue, this.TimerInterval, this.lblTimerCount.ClientID);

      ClientScript.RegisterStartupScript(this.GetType(), "TimerScript",
                                    bldr.ToString(), true);

      ClientScript.RegisterHiddenField("timerData", timerStartValue.ToString());

// Gets or Sets the Timer Value to the ViewState
private Int32 TimerInterval
    object o = ViewState["timerInterval"];
    if (o != null) { return Int32.Parse(o.ToString()); }
    return 50;
    ViewState["timerInterval"] = value; 

Hope this will be a help to start your project. The attachment is prepared as a template that you can use for your project and can extend to meet your requirements.


  • 13th September, 2010: Initial post


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


About the Author

Technical Lead
India India
I code, learn, listen, and some day in a near future be a proud farmer.

You may also be interested in...

Comments and Discussions

QuestionDatabase Pin
Waris Arain12-Jul-17 22:09
memberWaris Arain12-Jul-17 22:09 
GeneralMessage Closed Pin
7-Apr-17 10:26
memberKaren kumar7-Apr-17 10:26 
Questiondata base Pin
Member 124984333-May-16 0:51
memberMember 124984333-May-16 0:51 
QuestionNo DB structure in the project Pin
shailly verma10-Jan-16 20:23
membershailly verma10-Jan-16 20:23 
QuestionNo Database In The App Data Folder Pin
obaid ahmed(gleam)27-Mar-15 15:58
memberobaid ahmed(gleam)27-Mar-15 15:58 
Questionerror Pin
AnirbanM 216-Jan-15 23:43
memberAnirbanM 216-Jan-15 23:43 
GeneralMy vote of 4 Pin
Member 1065735615-Jun-14 6:46
memberMember 1065735615-Jun-14 6:46 
GeneralMy vote of 4 Pin
Rajesh198897-Apr-14 21:00
memberRajesh198897-Apr-14 21:00 
QuestionProblem with timer Pin
downloadsrk26-May-13 20:17
memberdownloadsrk26-May-13 20:17 
AnswerRe: Problem with timer Pin
VallarasuS26-May-13 20:26
memberVallarasuS26-May-13 20:26 
QuestionType or namespace ConnectToDb could not be found . Pin
aksrup17-May-13 4:45
memberaksrup17-May-13 4:45 
AnswerRe: Type or namespace ConnectToDb could not be found . Pin
VallarasuS19-May-13 21:02
memberVallarasuS19-May-13 21:02 
GeneralRe: Type or namespace ConnectToDb could not be found . Pin
aksrup20-May-13 1:19
memberaksrup20-May-13 1:19 
QuestionMaster Pages Pin
patiya25-Oct-12 2:04
memberpatiya25-Oct-12 2:04 
AnswerRe: Master Pages Pin
VallarasuS25-Oct-12 6:21
memberVallarasuS25-Oct-12 6:21 
QuestionDatabase download Pin
rahuljains18-Sep-12 21:31
memberrahuljains18-Sep-12 21:31 
AnswerRe: Database download Pin
Dannoman123415-Apr-14 3:32
memberDannoman123415-Apr-14 3:32 
QuestionTimer is not working when i added the scriptmanager tag in aspx page because of using ajax Pin
K.Mohamed Sujaludeen7-Sep-12 23:41
memberK.Mohamed Sujaludeen7-Sep-12 23:41 
QuestionRe: Timer is not working when i added the scriptmanager tag in aspx page because of using ajax Pin
vsanju6-Jun-14 0:18
membervsanju6-Jun-14 0:18 
QuestionNot find file solution? Pin
k56flex23-Aug-12 19:01
memberk56flex23-Aug-12 19:01 
AnswerRe: Not find file solution? Pin
VallarasuS23-Aug-12 19:28
memberVallarasuS23-Aug-12 19:28 
QuestionVB Version of the timer Pin
patiya17-Aug-12 6:58
memberpatiya17-Aug-12 6:58 
AnswerRe: VB Version of the timer Pin
VallarasuS20-Aug-12 20:45
memberVallarasuS20-Aug-12 20:45 
QuestionMessage Closed Pin
23-May-12 19:11
memberMember 869876723-May-12 19:11 
Questiondatabase for quiz Pin
mbjino15-Apr-12 21:27
membermbjino15-Apr-12 21:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180218.2 | Last Updated 13 Sep 2010
Article Copyright 2010 by VallarasuS
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid