Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I am developing an online test system.
I have developed a timer using timer control.
The problem is when I start the test the page is getting refreshed automatically.
I want to stop auto refresh of timer.
 

 
//.aspx code
 
<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick"
                    Interval="1000">
<asp:Label ID="label_time"
                runat="server" Text=""></asp:Label>
 
//.aspx.cs code
 
int totalSeconds = 0;
   int seconds = 0;
   int minutes = 0;
   string time = &amp;amp;quot;&amp;amp;quot;;
 

 
  protected void Page_Load(object sender, EventArgs e)
   {
 
           if (!Page.IsPostBack)
           {
               Session["time"] = 1800 ;
 

           }
 

      }
 

    protected void Timer1_Tick(object sender, EventArgs e)
   {
       Session["time"] = Convert.ToInt16(Session["time"]) - 1;
       if (Convert.ToInt16(Session["time"]) <= 0)
       {
 
          label_time.Text = "TimeOut!";
           insert_result();
           Response.Redirect("Show_result.aspx");
       }
 
       else
       {
           totalSeconds = Convert.ToInt16(Session["time"]);
            seconds = totalSeconds % 60;
            minutes = totalSeconds / 60;
            time = minutes + ":" + seconds;
           label_time.Text = time;
       }
   }
Posted 18-Feb-13 0:19am
Edited 18-Feb-13 2:13am
v6
Comments
Ankur\m/ at 18-Feb-13 5:24am
   
You have Timer's Tick event defined which means the code will get executed on every tick of the timer. That is why the page is getting refreshed. You should read how a timer control works first.
Gunjan Bhasin at 18-Feb-13 5:30am
   
sir I have gone through many links and tutorials and finally implemented this code. Without tick event how will the time get decremented ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If you are using time control then on tick event first of all you have to stop your timer in first line, then write line code which you want to execute in timer tick event, after that you have to start your timer in last line.
 
in between you want to stop your timer permanently, then you have to write Timer1.Enabled = false;
  Permalink  
Comments
Dhanashree Dive at 18-Feb-13 8:02am
   
why do we need to stop timer? and yes what does stop permanently means for? if i stop the timer permanently then how it will display the time?
Mr. Mahesh Patel at 18-Feb-13 8:19am
   
We have to stop timer because this event will occur at every time interval but my be possible that previous process may not completed. so system may get hang.
 
And yes, If you don't want to stop timer permanently, then don't do it dear.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
You should use your timer in UpdatePanel other than other controls on your page and if possible please use JavaScript timer so that it will not hit the server every time.
 
Please see this javascript code:-
 
<html>
<head>
<title>Countdown</title>
<script type="text/javascript">
// set minutes
var mins = 5;
 
// calculate the seconds (don't change this! unless time progresses at a different speed for you...)
var secs = mins * 60;
function countdown() {
setTimeout('Decrement()',1000);
}
function Decrement() {
if (document.getElementById) {
minutes = document.getElementById("minutes");
seconds = document.getElementById("seconds");
// if less than a minute remaining
if (seconds < 59) {
seconds.value = secs;
} else {
minutes.value = getminutes();
seconds.value = getseconds();
}
secs--;
setTimeout('Decrement()',1000);
}
}
function getminutes() {
// minutes is seconds divided by 60, rounded down
mins = Math.floor(secs / 60);
return mins;
}
function getseconds() {
// take mins remaining (as seconds) away from total seconds remaining
return secs-Math.round(mins *60);
}
</script>
</head>
<body>
 
<div id="timer">
This is only valid for the next <input id="minutes" type="text" style="width: 14px; border: none; background-color:none; font-size: 16px; font-weight: bold;"> minutes and <input id="seconds" type="text" style="width: 26px; border: none; background-color:none; font-size: 16px; font-weight: bold;"> seconds.
</div>
<script>
countdown();
</script>
 

 
Thanks
  Permalink  
Comments
Gunjan Bhasin at 18-Feb-13 6:55am
   
Satyendra kumar thanks for your advice but above code is not stoping the timer when it is coming on 0.it is counting in negative numbers also. please check code before pasting it from other website.
 

http://snipplr.com/view/51412/
   
I just tried to help you in my working time. So in hurry I gave you hint. As a developer if you did not get hints then check ur self. don't blame others who tried to help u....
Gunjan Bhasin at 26-Feb-13 1:10am
   
Please don't give any hint or solution in hurry(as mentioned by you). Your wrong hint can cause any damage to others project or they will waste time to implement your concept.

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



Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100