Click here to Skip to main content
13,189,802 members (65,310 online)
Rate this:
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:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick"
<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!";
           totalSeconds = Convert.ToInt16(Session["time"]);
            seconds = totalSeconds % 60;
            minutes = totalSeconds / 60;
            time = minutes + ":" + seconds;
           label_time.Text = time;
Posted 17-Feb-13 23:19pm
Updated 18-Feb-13 1:13am
Ankur\m/ 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 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 ?
Member 12107284 18-Nov-16 4:31am
Hello Gunjan Bhasin are you get the code for stop refreshinf if you get then plz send me
Rate this: bad
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;
Dhanashree Dive 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 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
Please Sign up or sign in to vote.

Solution 1


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:-

<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() {
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();
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);
<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.

Gunjan Bhasin 18-Feb-13 6:55am
Satyendra kumar thanks for your advice but above code is not stoping the timer when it is coming on is counting in negative numbers also. please check code before pasting it from other website.
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 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.171016.2 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2017
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