Click here to Skip to main content
Licence CPOL
First Posted 13 Sep 2010
Views 27,024
Downloads 6,420
Bookmarked 13 times

Online Exam in C# ASP NET

By | 13 Sep 2010 | Article
Online Examination Project with CountDown Timer

Introduction

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.

Background

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);
        
      bldr.Append("Timer.go()");

      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
{
  get
  {
    object o = ViewState["timerInterval"];
    if (o != null) { return Int32.Parse(o.ToString()); }
    return 50;
  }
  set
  {
    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.

History

  • 13th September, 2010: Initial post

License

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

About the Author

VallarasuS

Software Developer

India India

Member

Follow on Twitter Follow on Twitter
I code!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questiondatabase for quiz Pinmembermbjino20:27 15 Apr '12  
AnswerRe: database for quiz Pinmembermbjino20:29 15 Apr '12  
GeneralRe: database for quiz PinmemberVallarasuS22:47 15 Apr '12  
GeneralRe: database for quiz Pinmembermbjino3:01 16 Apr '12  
GeneralRe: database for quiz Pinmembertesstaco8:07 1 May '12  
QuestionTimer Pinmembersammaed19:10 29 Jan '12  
AnswerRe: Timer PinmemberVallarasuS4:03 30 Jan '12  
GeneralRe: Timer Pinmembersammaed6:32 30 Jan '12  
GeneralRe: Timer PinmemberVallarasuS7:05 30 Jan '12  
QuestionDATA base PinmemberRajeshkrathor19:40 28 Dec '11  
AnswerRe: DATA base PinmemberVallarasuS15:15 29 Dec '11  
GeneralMy vote of 4 Pinmemberkitupadma19:43 30 Oct '11  
GeneralRe: My vote of 4 Pinmembervallarasus4:18 17 Nov '11  
QuestionExam Questions in a SQLDB Pinmemberjpiscitelli4:09 20 Oct '11  
AnswerRe: Exam Questions in a SQLDB Pinmembervallarasus6:53 20 Oct '11  
GeneralRe: Exam Questions in a SQLDB Pinmemberjpiscitelli2:38 21 Oct '11  
GeneralMy vote of 2 Pinmembersurendramarri0:57 12 Jan '11  
GeneralMy vote of 4 PinmemberHardikKSavaliya3:42 6 Dec '10  
GeneralMy vote of 3 Pinmembersaxenaabhi619:21 13 Sep '10  
GeneralRe: My vote of 3 Pinmembervallarasus8:55 1 Jun '11  
GeneralMy vote of 1 Pinmemberj03x27:31 13 Sep '10  
GeneralMy vote of 2 PinmentorSandeep Mewara5:41 28 Aug '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.5.120517.1 | Last Updated 13 Sep 2010
Article Copyright 2010 by VallarasuS
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid