|
|
|
| Web Hosting Guide |
![]() ![]() |
C#.NET: Web Timer Control Tutorial, C# & VB.NET |
Jul 5 2005, 06:09 PM
Post
#1
|
|
|
Member [ Level 1 ] Group: Members Posts: 35 Joined: 10-June 05 Member No.: 6,038 |
Web Timer Control C#
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()); } } } 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 } } 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 Adding your own code to handle the event in the appropriate place. Rich This post has been edited by microscopic^earthling: Jul 6 2005, 04:37 AM |
|
|
|
Mar 25 2008, 10:44 AM
Post
#2
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 0 Joined: 1-November 07 Member No.: 25,869 |
while creating threads me got disconnected from the controle
C#.NET: Web Timer Control Tutorial While making to the chat server once making connection with the ip of the local server me getting output only one at time is this is the timer problem. -reply by vishav |
|
|
|
Apr 16 2008, 06:32 PM
Post
#3
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 0 Joined: 1-November 07 Member No.: 25,869 |
yemen- thmar unv
C#.NET: Web Timer Control Tutorial I abdulkreem I create programm to send message by email But this program failure in this task When I pressed on send command show this message Do't able to connect in server Please help me this my genuration project This my programm Using System; Using System.Collections.Generic; Using System.ComponentModel; Using System.Data; Using System.Drawing; Using System.Text; Using System.Windows.Forms; Using System.Collections; Using System.Web; Using System.Web.SessionState; Using System.Web.UI; Using System.Web.UI.WebControls; Using System.Web.UI.HtmlControls; Using System.Web.Mail; MailMessage myMail = null; try { myMail = new MailMessage(); myMail.From = "ahmed_zoher95@yahoo.Com"; myMail.To ="abdulkreem_2008@yahoo.Com"; myMail.Subject ="GOOD"; myMail.Body = "hello"; SmtpMail.SmtpServer = "SMTP.Yahoo.Com"; SmtpMail.Send(myMail); statusLabel.Text = "Mail has been sent"; } catch (Exception ee) { MessageBox.Show(ee.Message); } -question by abdulkreem |
|
|
|
Sep 30 2008, 07:09 PM
Post
#4
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 1 Joined: 30-September 08 Member No.: 32,991 |
|
|
|
|
Jul 15 2009, 08:48 PM
Post
#5
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 0 Joined: 1-November 07 Member No.: 25,869 |
Great Job!
C#.NET: Web Timer Control Tutorial Thanks. I created an Ajax Server Control and just dropped your code in and replaced what was generated. Worked the first time -reply by John Dalnes |
|
|
|
![]() ![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
20 | jedipi | 6,363 | 8th March 2010 - 06:51 PM Last post by: iG- |
|||
![]() |
18 | Dream | 16,301 | 8th March 2010 - 01:13 AM Last post by: iG-Wuddup |
|||
![]() |
22 | mzwebfreak | 15,058 | 21st February 2010 - 11:44 PM Last post by: iG-Heather |
|||
![]() |
4 | Klaas | 2,927 | 16th February 2010 - 09:11 PM Last post by: iG-mubassir ansaro |
|||
![]() |
15 | jedipi | 3,787 | 13th February 2010 - 02:09 PM Last post by: magiccode9 |
|||
![]() |
10 | tansqrx | 8,282 | 9th February 2010 - 12:50 PM Last post by: iG-gagan deep singh |
|||
![]() |
6 | dhanesh | 2,776 | 4th February 2010 - 10:58 AM Last post by: iG-Sam |
|||
![]() |
10 | Snake | 3,767 | 21st January 2010 - 08:39 AM Last post by: iG-Fer9us0n |
|||
![]() |
11 | mimi_m | 9,713 | 16th January 2010 - 05:00 PM Last post by: iG-Tom Clubs |
|||
![]() |
17 | r3d | 8,332 | 6th January 2010 - 03:29 PM Last post by: iG-Steve |
|||
![]() |
12 | soleimanian | 5,757 | 6th January 2010 - 11:08 AM Last post by: iG-trlkly |
|||
![]() |
2 | turbopowerdmaxsteel | 7,991 | 4th January 2010 - 07:29 AM Last post by: iG-v_tarasu |
|||
![]() |
13 | dhanesh | 6,648 | 16th December 2009 - 11:02 AM Last post by: iG-ZeN |
|||
![]() |
1 | turbopowerdmaxsteel | 8,376 | 9th December 2009 - 10:22 AM Last post by: iG-imran |
|||
![]() |
28 | HTML_Guru | 5,078 | 8th December 2009 - 10:43 PM Last post by: HannahI |
|||
|
Lo-Fi Version | Time is now: 20th March 2010 - 01:00 PM |
© 2010 AstaHost: Free Web Hosting & Technical Discussion, Free Web Hosting. a member of xisto.
Powered by Invision Board. Skin: IPB Forum Skins
Expand / Collapse Navigation



Jul 5 2005, 06:09 PM






