Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
<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
C#
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
Updated 18-Feb-13 1:13am
v6
Comments
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

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;
 
Share this answer
 
Comments
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.
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:-

XML
<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
 
Share this answer
 
Comments
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 0.it is counting in negative numbers also. please check code before pasting it from other website.


http://snipplr.com/view/51412/
Satyendra Kumar(Analyst Programmer) 25-Feb-13 3:55am    
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)



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