tags:

views:

158

answers:

5

hello

I am developing online exam application using asp.net, in the start exam page i have created a javascript countdown timer, how can i move to next page automaticaly after timer reaches 00 and here is my code

long timerStartValue = 1000 ;

private int TimerInterval
{
    get
    {
        int o =(int) ViewState["timerInterval"];
        if(o==0)
        {

            return (o);
        }
        return 50 ;
    }
    set
    {
        ViewState["timerInterval"] = value;

    }
}

protected void Page_PreInit(object sender,EventArgs e)
{
    string timerVal = Request.Form["timerData"];
    if(! String.IsNullOrEmpty(timerVal))
    {
        timerVal = timerVal.Replace(",", String.Empty) ;
        this.timerStartValue = long.Parse(timerVal);

    } 
}

protected void Page_Load(object sender, EventArgs e)
{
  if(! IsPostBack)
  {
      this.timerStartValue = 10000; //3599000;//14400000;
        this.TimerInterval = 500;
   }
}
protected void Button1_Click(object sender, EventArgs e)
{
    this.timerStartValue = 3599000;
}

protected void Page_PreRender(object sender, EventArgs e) 
{
   System.Text.StringBuilder bldr=new System.Text.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());
}

thanx in advance sangita

A: 

This should do it:

http://forums.asp.net/t/1052648.aspx

Robert Harvey
A: 

It sounds like when you click the "Next" button, you are loading an entirely new page. This of course changes all the content and resets all the javascript. You can't maintain state across pages without a bit of work.

The solution to this could be to save the timer state when the next button is pressed, and pass it to the next stage. You could do this by saving the timer state to a hidden form input and submitting it along with the Next button.

The other option would be to load your questions via AJAX. Instead of moving to a new page every time the next button is clicked, you could simply replace the question portion of the page with a new question, and leave the timer intact. This is probably the solution I would use.

zombat
A: 

Are u reloading the entire page when clicking on the next button ? That may leads to realod the java script file also.So the variable values will reset.May be you can think about showing the questions /answers via Ajax.You need not reload the entire page when showing the next question.the part when you show the quiz will only be updated.so you can maintain the global variables in your java script too. Check the below link to know about partial page updating using jQuery.

http://www.west-wind.com/presentations/jquery/jquerypart2.aspx

Hope this helps.

Shyju
A: 

You can put the timer in an iframe if you can't get rid of the postback.

bashmohandes
A: 

You need a way to persist information between pages, and there's really only one possibility: To make it part of the next page request.

Now, this could be subdivided into 2 categories:

1) As part of the url: http://www.example.com/page?timer=123;

2) As part of the headers;

And number 2 opens new possibilities:

a) As part of POST data;

b) As a client-side cookie only;

c) As a cookie tied to information on the server;

Number 1, 2a and 2b can be manipulated by the user. So what you can do is store some value in a cookie, a hash for example or a database row ID, that you'll use to fetch information on the server.

tl;dr? Use a asp "Session object". It lets you keep things on the server-side and users will have no idea what they are.

inerte

related questions