Click here to Skip to main content
Click here to Skip to main content

Online Exam in C# ASP NET

By , 13 Sep 2010
 

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
I code, learn, listen, and some day in a near future be a proud farmer.

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionType or namespace ConnectToDb could not be found .memberaksrup17 May '13 - 3:45 
AnswerRe: Type or namespace ConnectToDb could not be found .memberVallarasuS19 May '13 - 20:02 
GeneralRe: Type or namespace ConnectToDb could not be found .memberaksrup20 May '13 - 0:19 
QuestionMaster Pagesmemberpatiya25 Oct '12 - 1:04 
AnswerRe: Master PagesmemberVallarasuS25 Oct '12 - 5:21 
QuestionDatabase downloadmemberrahuljains18 Sep '12 - 20:31 
QuestionTimer is not working when i added the scriptmanager tag in aspx page because of using ajaxmemberK.Mohamed Sujaludeen7 Sep '12 - 22:41 
QuestionNot find file solution?memberk56flex23 Aug '12 - 18:01 
AnswerRe: Not find file solution?memberVallarasuS23 Aug '12 - 18:28 
QuestionVB Version of the timermemberpatiya17 Aug '12 - 5:58 
AnswerRe: VB Version of the timermemberVallarasuS20 Aug '12 - 19:45 
Questiononlinexams.inmemberMember 869876723 May '12 - 18:11 
Questiondatabase for quizmembermbjino15 Apr '12 - 20:27 
AnswerRe: database for quizmembermbjino15 Apr '12 - 20:29 
GeneralRe: database for quizmemberVallarasuS15 Apr '12 - 22:47 
GeneralRe: database for quizmembermbjino16 Apr '12 - 3:01 
GeneralRe: database for quizmembertesstaco1 May '12 - 8:07 
QuestionTimermembersammaed29 Jan '12 - 19:10 
AnswerRe: TimermemberVallarasuS30 Jan '12 - 4:03 
GeneralRe: Timermembersammaed30 Jan '12 - 6:32 
Thanks ....
 
1. I just found the small timer program in C# - Its been working to my requirement.
 
2. Its like ....their are 4 radio buttons for each answers - and their in a question button also .Each ques has each button.
So totally we have 10 - 15 questions (buttons on the right hand side of the website)
 
When we finally check before submitting the quiz, need to check all the questions have been answers(Need to check all the button color)
GeneralRe: TimermemberVallarasuS30 Jan '12 - 7:05 
GeneralRe: TimermemberMember 994730327 Mar '13 - 17:32 
QuestionDATA basememberRajeshkrathor28 Dec '11 - 19:40 
AnswerRe: DATA basememberVallarasuS29 Dec '11 - 15:15 
GeneralMy vote of 4memberkitupadma30 Oct '11 - 19:43 
GeneralRe: My vote of 4membervallarasus17 Nov '11 - 4:18 
QuestionExam Questions in a SQLDBmemberjpiscitelli20 Oct '11 - 4:09 
AnswerRe: Exam Questions in a SQLDBmembervallarasus20 Oct '11 - 6:53 
GeneralRe: Exam Questions in a SQLDBmemberjpiscitelli21 Oct '11 - 2:38 
GeneralRe: Exam Questions in a SQLDBmemberMember 963246926 Nov '12 - 11:19 
GeneralMy vote of 2membersurendramarri12 Jan '11 - 0:57 
GeneralMy vote of 4memberHardikKSavaliya6 Dec '10 - 3:42 
GeneralMy vote of 3membersaxenaabhi613 Sep '10 - 19:21 
GeneralRe: My vote of 3membervallarasus1 Jun '11 - 8:55 
GeneralMy vote of 1memberj03x213 Sep '10 - 7:31 
GeneralMy vote of 2mentorSandeep Mewara28 Aug '10 - 5:41 

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

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