Click here to Skip to main content
13,256,260 members (47,975 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

GeneralRe: database for quiz Pin
tesstaco1-May-12 9:07
membertesstaco1-May-12 9:07 
QuestionTimer Pin
sammaed29-Jan-12 20:10
membersammaed29-Jan-12 20:10 
AnswerRe: Timer Pin
VallarasuS30-Jan-12 5:03
memberVallarasuS30-Jan-12 5:03 
GeneralRe: Timer Pin
sammaed30-Jan-12 7:32
membersammaed30-Jan-12 7:32 
GeneralRe: Timer Pin
VallarasuS30-Jan-12 8:05
memberVallarasuS30-Jan-12 8:05 
GeneralRe: Timer Pin
Member 994730327-Mar-13 18:32
memberMember 994730327-Mar-13 18:32 
QuestionDATA base Pin
Rajeshkrathor28-Dec-11 20:40
memberRajeshkrathor28-Dec-11 20:40 
AnswerRe: DATA base Pin
VallarasuS29-Dec-11 16:15
memberVallarasuS29-Dec-11 16:15 
Actual implementation should go inside ConnectToDb.cs -> GetDataSet() method which will populate the questions from db, for demonstration I've added a dummy dataset within the method, so source with this article does not requires a db to execute.
Regards Vallarasu S |

GeneralMy vote of 4 Pin
kitupadma30-Oct-11 20:43
memberkitupadma30-Oct-11 20:43 
GeneralRe: My vote of 4 Pin
vallarasus17-Nov-11 5:18
membervallarasus17-Nov-11 5:18 
QuestionExam Questions in a SQLDB Pin
jpiscitelli20-Oct-11 5:09
memberjpiscitelli20-Oct-11 5:09 
AnswerRe: Exam Questions in a SQLDB Pin
vallarasus20-Oct-11 7:53
membervallarasus20-Oct-11 7:53 
GeneralRe: Exam Questions in a SQLDB Pin
jpiscitelli21-Oct-11 3:38
memberjpiscitelli21-Oct-11 3:38 
GeneralRe: Exam Questions in a SQLDB Pin
Member 963246926-Nov-12 12:19
memberMember 963246926-Nov-12 12:19 
GeneralMy vote of 2 Pin
surendramarri12-Jan-11 1:57
membersurendramarri12-Jan-11 1:57 
GeneralMy vote of 4 Pin
HardikKSavaliya6-Dec-10 4:42
memberHardikKSavaliya6-Dec-10 4:42 
GeneralMy vote of 3 Pin
saxenaabhi613-Sep-10 20:21
membersaxenaabhi613-Sep-10 20:21 
GeneralRe: My vote of 3 Pin
vallarasus1-Jun-11 9:55
membervallarasus1-Jun-11 9:55 
GeneralMy vote of 1 Pin
j03x213-Sep-10 8:31
memberj03x213-Sep-10 8:31 
GeneralMy vote of 2 Pin
Sandeep Mewara28-Aug-10 6:41
mentorSandeep Mewara28-Aug-10 6:41 

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
Web02 | 2.8.171114.1 | Last Updated 13 Sep 2010
Article Copyright 2010 by VallarasuS
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid