How To Use Timer in ASP.NET
In this tutorial I am going to show you, how to use Timer Control in asp.net.
I am not going to discuss about the theoretical knowledge of Timer control here i just show how i use it in my Quiz Application.
So here i am showing you step by step procedure to use a timer control in asp.net.
So lets Start..
So here i am showing you step by step procedure to use a timer control in asp.net.
So lets Start..
1. Copy and Paste this code ::
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick">
</asp:Timer>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div class="tictic">
<table class="style1">
<tr>
<td align="center"
style="color: #000000; font-family: tahoma; font-size: large;">
Time Left</td>
</tr>
<tr>
<td align="center"
style="color: #000000; font-family: tahoma; font-size: large;">
<asp:Label ID="Label22" runat="server"></asp:Label>
:<asp:Label ID="Label23" runat="server"></asp:Label>
:<asp:Label ID="Label24" runat="server"></asp:Label>
</td>
</tr>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
the design view of the above code is like this snapshot.
2. Copy and Paste this code in .CS file ::
int ss, m, h;
string s;
int mm = 0; //marks
int em = 0; //non attempted
int wr = 0; //wrong answer
float res; //result
private void MsgBox(String Message)
{
string alertMessage = "<script language=JavaScript>";
alertMessage += "alert('" + Message + "');";
alertMessage += "</script>";
Page.RegisterStartupScript("MsgBox", alertMessage);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string s = "0:30:0";
int end = s.LastIndexOf(":");
int start = s.IndexOf(":");
string sec = s.Substring(end + 1);
string min = s.Substring(start + 1, end - start - 1);
string hr = s.Substring(0, start);
ss = Convert.ToInt32(sec);
m = Convert.ToInt32(min);
h = Convert.ToInt32(hr);
Label22.Text = h.ToString();
Label23.Text = m.ToString();
Label24.Text = ss.ToString();
}
// MultiView1.ActiveViewIndex = 0;
}
// start button event
protected void Button1_Click(object sender, EventArgs e){
Timer1.Enabled = true;
Timer1.Interval = 1000;
Timer1.Tick += new EventHandler<EventArgs>(Timer1_Tick);
MultiView1.ActiveViewIndex = 1;
}
// timer's tick event
protected void Timer1_Tick(object sender, EventArgs e)
{
ss = Convert.ToInt32(Label24.Text);
if (ss > 0)
{
ss = ss - 1;
Label24.Text = ss.ToString();
}
else if (ss <= 0)
{
m = Convert.ToInt32(Label23.Text);
if (m == 0)
{
h = Convert.ToInt32(Label22.Text);
if (h > 0)
{
h = h - 1;
m = 59;
ss = 59;
Label23.Text = m.ToString();
Label22.Text = h.ToString();
Label24.Text = ss.ToString();
}
else
{
MsgBox("Time up.");
Timer1.Enabled = false;
// your code here
}
}
else if (m > 0)
{
m = m - 1;
ss = 59;
Label23.Text = m.ToString();
Label24.Text = ss.ToString();
}
}
}
Hope this will help tou if have any problem you can commnet it.
How To Use Timer in ASP.NET
Reviewed by Team tt24
on
7:16 AM
Rating:
ssssss
ReplyDelete