When we add a timer control to a web page it will add the following script to the page.

  1. <script type="text/javascript" >
  2. //<![CDATA[
  3. Sys.Application.add_init(function() {
  4. $create(Sys.UI._Timer, {"enabled":true,"interval":5000,"uniqueID":"Timer1"}, null, null, $get("Timer1"));
  5. });
  6. //]]>
  7. </script>
Now in aspx page, I was refreshing page after fixed time using timer control. But the problem was that when I was performing certain actions the page would refresh and this behavior is not as expected. While performing any operations page should not refresh.

To solve this issue I stopped the timer control using JavaScript and when the task is completed again I start the timer control. In my example I will open a modal popup from Gridview and it closes after page refresh .The various methods available for timer control at client side using JavaScript are:

  • For stop timer control tick event - timer._stopTimer();
  • For start timer control tick event - timer._startTimer();
  • For change interval of timer - timer.set_interval(5000);
  • For checking whether timer is enabled - timer.get_enabled();
  • For enabling and disabling timer -timer.setenabled(true); true for enabling and false for disabling.

Below is the code used to solve this issue.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="StopTimer.aspx.cs" Inherits="StopTimer" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <meta charset="utf-8">
  7. <meta name="viewport" content="width=device-width, initial-scale=1">
  8. <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  9. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  10. <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  11. <script>
  12. function showpopup()
  13. {
  14. var timer = $find('<%= Timer1.ClientID %>');
  15. timer._stopTimer();
  16. console.log("Timer stopped");
  17. $('#myModal').modal('show');
  18. }
  19. $(document).ready(function () {
  20. $(".custom-close").on('click', function () {
  21. var timer1 = $find('<%= Timer1.ClientID %>');
  22. var value = $('#<%=DropDownList1.ClientID %>').val();
  23. timer1.set_interval(parseInt(value));
  24. timer1._startTimer();
  25. $('#myModal').modal('hide');
  26. }
  27. )
  28. });
  29. </script>
  30. </head>
  31. <body>
  32. <form id="form1" runat="server">
  33. <div>
  34. <!-- Modal -->
  35. <div class="modal fade" id="myModal" role="dialog">
  36. <div class="modal-dialog">
  37. <!-- Modal content-->
  38. <div class="modal-content">
  39. <div class="modal-header">
  40. <button type="button" class="custom-close" >×</button>
  41. <h4 class="modal-title">Modal Header</h4>
  42. </div>
  43. <div class="modal-body">
  44. <p>Some text in the modal.</p>
  45. </div>
  46. <div class="modal-footer">
  47. <button type="button" class="btn btn-default" >Close</button>
  48. </div>
  49. </div>
  50. </div>
  51. </div>
  52. <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
  53. <asp:ListItem Text="Refresh Every 5 sec" Value="5000" Selected="True"></asp:ListItem>
  54. <asp:ListItem Text="Refresh Every 1 Min" Value="60000" ></asp:ListItem>
  55. <asp:ListItem Text="Refresh Every 2 Min" Value="120000"></asp:ListItem>
  56. <asp:ListItem Text="Refresh Every 3 Min" Value="180000"></asp:ListItem>
  57. </asp:DropDownList>
  58. <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick"></asp:Timer>
  59. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
  60. <Columns>
  61. <asp:TemplateField HeaderText="Price">
  62. <ItemTemplate>
  63. <asp:Label ID="Label1" runat="server" CssClass="btn " onclick="showpopup();" Text='<% #Eval("Price") %>'></asp:Label>
  64. </ItemTemplate>
  65. </asp:TemplateField>
  66. <asp:TemplateField HeaderText="Datetransaction">
  67. <ItemTemplate>
  68. <asp:Label ID="Label2" runat="server" Text='<% #Eval("Datetransaction") %>'></asp:Label>
  69. </ItemTemplate>
  70. </asp:TemplateField>
  71. <asp:TemplateField HeaderText="Productcode">
  72. <ItemTemplate>
  73. <asp:Label ID="Label3" runat="server" Text='<% #Eval("Productcode") %>'></asp:Label>
  74. </ItemTemplate>
  75. </asp:TemplateField>
  76. </Columns>
  77. </asp:GridView>
  78. </div>
  79. </form>
  80. </body>
  81. </html>
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. public partial class StopTimer : System.Web.UI.Page
  10. {
  11. string connStr = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. Timer1.Interval = Convert.ToInt32(DropDownList1.SelectedValue);
  15. if (!IsPostBack)
  16. grdibind();
  17. }
  18. protected void grdibind()
  19. {
  20. SqlConnection con = new SqlConnection(connStr);
  21. SqlCommand cmd = new SqlCommand();
  22. cmd.CommandText = "select top 1000 * from Sales";
  23. cmd.Connection = con;
  24. con.Open();
  25. GridView1.DataSource = cmd.ExecuteReader();
  26. GridView1.DataBind();
  27. con.Close();
  28. }
  29. protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  30. {
  31. Timer1.Interval = Convert.ToInt32(DropDownList1.SelectedValue);
  32. }
  33. protected void Timer1_Tick(object sender, EventArgs e)
  34. {
  35. grdibind();
  36. }
  37. }
On Clicking any cell of the Price column a popup will open using showpopup() thereby stopping timer control using _stopTimer(). When close of Modal popup again i call _startTimer().