Difficulty: Easy
Time: 10 Minutes
The aim of this tutorial is to create a Timer that works on web pages.
Timers are a very useful control in Windows Applications but how can you use them in web pages. With the default controls you get in the .Net Framework there is not a Web Timer.
This web timer can be used as a normal web control you will be able to drag and drop it onto Web Forms, and modify it using the properties window.
This Timer Works using the JavaScript setTimeout() function which once the required time has elapsed unlike a Windows Forms Timer it post backs the page and then the Page_Load Event will handle the event.
Here is the code:
CODE
using System;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebControls
{
[DefaultProperty("TimerInterval")]
[ToolboxData("<{0}:TimerControl runat=server></{0}:TimerControl>")]
public class TimerControl : System.Web.UI.Control, INamingContainer
{
[Browsable(true)]
[Category("Behavior")]
[DefaultValue("TimerPostBack")]
[Description("The PostBack value.")]
public string PostBackValue
{
get
{
object viewState = this.ViewState["PostBackValue"];
return (viewState == null) ? "TimerPostBack" : (string)viewState;
}
set { this.ViewState["PostBackValue"] = value; }
}
[Browsable(true)]
[Category("Behavior")]
[DefaultValue(1000)]
[Description("The timer interval, in milli-seconds (1000 = 1 second).")]
public double TimerInterval
{
get
{
object viewState = this.ViewState["TimerInterval"];
return (viewState == null) ? 1000 : (double)viewState;
}
set { this.ViewState["TimerInterval"] = value; }
}
protected override void OnPreRender(EventArgs e)
{
StringBuilder sbScript = new StringBuilder();
sbScript.Append("\n<script language=\"JavaScript\" type=\"text/javascript\">\n");
sbScript.Append("<!--\n");
sbScript.Append("\n");
sbScript.Append("function autoPostBackTimer()\n");
sbScript.Append("{\n");
sbScript.Append(" " + this.Page.GetPostBackEventReference(this, this.PostBackValue) + ";");
sbScript.Append("}\n");
sbScript.Append("// -->\n");
sbScript.Append("</script>\n");
this.Page.RegisterClientScriptBlock(
"TimerControlScript", sbScript.ToString());
StringBuilder sbScript2 = new StringBuilder();
sbScript2.Append("\n<script language=\"JavaScript\" type=\"text/javascript\">\n");
sbScript2.Append("<!--\n");
sbScript2.Append("window.setTimeout('autoPostBackTimer()', " + this.TimerInterval.ToString() + ")\n");
sbScript2.Append("// -->\n");
sbScript2.Append("</script>\n");
this.Page.RegisterStartupScript(
"TimerControlStartupScript", sbScript2.ToString());
}
}
}
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebControls
{
[DefaultProperty("TimerInterval")]
[ToolboxData("<{0}:TimerControl runat=server></{0}:TimerControl>")]
public class TimerControl : System.Web.UI.Control, INamingContainer
{
[Browsable(true)]
[Category("Behavior")]
[DefaultValue("TimerPostBack")]
[Description("The PostBack value.")]
public string PostBackValue
{
get
{
object viewState = this.ViewState["PostBackValue"];
return (viewState == null) ? "TimerPostBack" : (string)viewState;
}
set { this.ViewState["PostBackValue"] = value; }
}
[Browsable(true)]
[Category("Behavior")]
[DefaultValue(1000)]
[Description("The timer interval, in milli-seconds (1000 = 1 second).")]
public double TimerInterval
{
get
{
object viewState = this.ViewState["TimerInterval"];
return (viewState == null) ? 1000 : (double)viewState;
}
set { this.ViewState["TimerInterval"] = value; }
}
protected override void OnPreRender(EventArgs e)
{
StringBuilder sbScript = new StringBuilder();
sbScript.Append("\n<script language=\"JavaScript\" type=\"text/javascript\">\n");
sbScript.Append("<!--\n");
sbScript.Append("\n");
sbScript.Append("function autoPostBackTimer()\n");
sbScript.Append("{\n");
sbScript.Append(" " + this.Page.GetPostBackEventReference(this, this.PostBackValue) + ";");
sbScript.Append("}\n");
sbScript.Append("// -->\n");
sbScript.Append("</script>\n");
this.Page.RegisterClientScriptBlock(
"TimerControlScript", sbScript.ToString());
StringBuilder sbScript2 = new StringBuilder();
sbScript2.Append("\n<script language=\"JavaScript\" type=\"text/javascript\">\n");
sbScript2.Append("<!--\n");
sbScript2.Append("window.setTimeout('autoPostBackTimer()', " + this.TimerInterval.ToString() + ")\n");
sbScript2.Append("// -->\n");
sbScript2.Append("</script>\n");
this.Page.RegisterStartupScript(
"TimerControlStartupScript", sbScript2.ToString());
}
}
}
Just Copy and Paste it into C# Web Application or C# Web Control Project and build it.
Then if you have made a web control project you may want to compile it and then add it to the Visual Studio tool box.
To actually use the Web Timer Drag and Drop it onto a Web Form set the Time (1 sec = 1000), and the Post Back value, and then in the Page_Load event add this
C#
CODE
private void Page_Load(object sender, System.EventArgs e)
{
if ( this.IsPostBack )
{
object eventArgument = this.Request["__EVENTARGUMENT"];
if ( (eventArgument != null) && (eventArgument.ToString().Trim() == this.TimerControl1.PostBackValue) )
{
//Function or code to handle the elapse of the timer.
}
else
{
// Do Nothing
}
}
{
if ( this.IsPostBack )
{
object eventArgument = this.Request["__EVENTARGUMENT"];
if ( (eventArgument != null) && (eventArgument.ToString().Trim() == this.TimerControl1.PostBackValue) )
{
//Function or code to handle the elapse of the timer.
}
else
{
// Do Nothing
}
}
VB
CODE
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Page.IsPostBack Then
Dim eventarguement As Object = Me.Request("__EVENTARGUMENT")
If Not eventarguement = Nothing And CStr(eventarguement).Trim() = Me.TimerControl1.PostBackValue Then
'Code to Handle event
Else
' Do Nothing
End If
End If
End Sub
If Page.IsPostBack Then
Dim eventarguement As Object = Me.Request("__EVENTARGUMENT")
If Not eventarguement = Nothing And CStr(eventarguement).Trim() = Me.TimerControl1.PostBackValue Then
'Code to Handle event
Else
' Do Nothing
End If
End If
End Sub
Adding your own code to handle the event in the appropriate place.
Rich

