Click here to Skip to main content
15,895,370 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have two page(question and result).,In first page(question) i'm displaying question and options for the question... and iam displaying the next question via button click function(it fetches the next qusetion from the database) ... now i want to add timer for the whole set of questions..once timer value decreses to '0' it should redirect to result page..my problem is whenever i clicks the next button to show next question the timer values is starting from original value.. i want the timer to continue from last value not from initial value..help me
Posted
Updated 19-Mar-13 21:40pm
v4
Comments
Ramkumar K 20-Mar-13 4:24am    
pls help me
Ankur\m/ 20-Mar-13 4:56am    
Which version of .Net framework are you using? ASP.NET do not have inbuilt timer control until ver 4.0
Or are you using AJAX extension Timer Control?

As ASP.NET is stateless, it doesn't maintain the state of timer as well. :(

So you need to implement State Management technique.

The following is the example of state management using ViewState:

C#
protected void Page_Load(object sender, EventArgs e)
{
     if(ViewState["counter"] != null)
         timer1.Interval = (int)ViewState["counter"];
     else
      {
         timer1.Interval = 30000; // Assuming the total time of the exam is 30 secs
          ViewState["countet"]=30000;
       }
}


Please feel free to ask any doubt if you have. :)
 
Share this answer
 
v2
Comments
Ramkumar K 20-Mar-13 7:47am    
How can i store the value of timer into "ViewState["counter"]" before postback..
Chinmaya C 20-Mar-13 14:07pm    
Try with the following code:

write the following page design:
<head runat="server">
<title></title>
<script type="text/javascript" language="javascript">
var remain;
function countDown() {
displayTimer(remain);
remain = remain - 1;
if (remain == 0) {
window.location = "result.aspx";
}
else {
document.getElementById("hdnTimeRemained").value = remain;
setTimeout("countDown()", 1000);
}
}
function readCounter() {
remain = document.getElementById("hdnTimeRemained").value;
countDown();
}
function displayTimer(secs) {
var hr = Math.floor(secs / 3600);
secs = secs % 3600;
var min = Math.floor(secs / 60);
var sec = secs % 60;

document.getElementById("lblTime").innerHTML = hr + ":" + min + ":" + sec;
}
</script>
</head>
<body önload="readCounter();">
<form id="form1" runat="server">
<div id="lblTime">
</div>
<asp:HiddenField ID="hdnTimeRemained" runat="server" />
<div>
<asp:Button ID="btnNext" runat="server" Text="Next" />
</div>
</form>
</body>
</html>

In the code behind's page_load event write the following:

protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
hdnTimeRemained.Value = "3600";
}

hope this will help... :)
I think you can you can use AJAX to implement desired functionality.

Please go through this link :

AJAX Timer[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900