HOW TO GENERATE SELF TIMER IN ASP.NET : -
========================================
.cs file code:-
-----------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Timers;
public partial class Stop_Watch : System.Web.UI.Page
{
public int time_second;
public int time_min;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
Timer1.Enabled = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
Timer1.Enabled = true;
btnpause.Visible = true;
btnreset.Visible = true;
}
protected void Timer1_Tick(object sender, EventArgs e)
{
time_second = int.Parse(lblsec1.Text);
time_second = time_second + 1;
lblsec1.Text = time_second.ToString();
if (lblsec1.Text == "10")
{
lblsec2.Visible = false;
}
if (lblmin1.Text == "10")
{
lblmin2.Visible = true;
}
if (lblsec1.Text == "60")
{
lblsec2.Visible = true;
time_min = int.Parse(lblmin1.Text);
lblsec1.Text = "0";
time_min = time_min + 1;
lblmin1.Text = time_min.ToString();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Timer1.Enabled = false;
}
protected void Button3_Click(object sender, EventArgs e)
{
time_second = 0;
time_min = 0;
lblmin1.Text = "0";
lblsec1.Text = "0";
lblsec2.Visible = true;
Timer1.Enabled = false;
}
}
----------------------------------------------------------------------------------
.aspx page code :-
------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Stop_Watch.aspx.cs" Inherits="Stop_Watch" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 140px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
/* First you have to insert this SCRIPT MANAGER after form tag. without this, any ajax control doesn't work. */
</asp:ScriptManager>
<div>
<table class="style1">
<tr>
<td class="style2">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
/* You have to insert this UPLOAD PANEL.The timer will insert under this UPLOAD PANEL.This will help to refresh only partially like Google search box. */
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Enabled="false" Interval="1000"
ontick="Timer1_Tick">
</asp:Timer>
<asp:Label ID="lblmin2" runat="server" Text="0"></asp:Label>
<asp:Label ID="lblmin1" runat="server" Text="0"></asp:Label>
<asp:Label ID="Label3" runat="server" Text=":"></asp:Label>
<asp:Label ID="lblsec2" runat="server" Text="0"></asp:Label>
<asp:Label ID="lblsec1" runat="server" Text="0"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</td>
<td>
<asp:Button ID="btnstart" runat="server" OnClientClick ="startTimer()" Text="Start"
Width="160px" onclick="Button1_Click" />
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
<asp:Button ID="btnpause" runat="server" Text="Pause"
OnClientClick = "stopTimer()" onclick="Button2_Click" Width="76px" />
<asp:Button ID="btnreset" runat="server" Text="Reset" onclick="Button3_Click"
Width="79px" />
</td>
</tr>
</table>
</div>
</form>
<script type = "text/javascript">
function startTimer() {
return $find('Timer1')._startTimer();
}
function stopTimer() {
return $find('Timer1')._stopTimer();
}
</script>
</body>
</html>
=============================================================================