Click here to Skip to main content
15,900,254 members
Articles / Web Development / ASP.NET
Article

WebTimer - a server control, (setTimeout made easy)

Rate me:
Please Sign up or sign in to vote.
3.35/5 (16 votes)
12 Mar 20074 min read 168.3K   1.4K   45   30
Auto postback after n millisecs, generates a little bit of client side code.

Introduction

This small server control is not my idea, I was browsing the net looking for nothing in particular until I stumbled across a site that sold server controls for ASP.NET. One of those controls was a WebTimer control, I looked at it for 3 seconds and thought, why not make one my self, so I did.

I'm not going to explain in detail how the code works but for advanced and intermediate developers it should be pretty straight forward.

Down to the code

Ok, let's start, open a new "web control library" project in VS.NET. Give a suitable name, lets say WebTimerProject, after the project opens delete the file that VS generates and create a new one (so you can name it properly). Add new item - Web Custom Control, name it... WebTimer. The first lines of code after the namespace are:

C#
[DefaultProperty("Text"), 

ToolboxData("<{0}:WebCustomControl1 runat="server"></{0}:WebCustomControl1>")]

public class WebCustomControl1 : System.Web.UI.WebControls.WebControl

The default property is defined here; we will change it to the ID property.

C#
[DefaultProperty("ID"),
  ToolboxData("<{0}:WebTimer runat="server"></{0}:WebTimer>")]

[DefaultEvent("IntervalExpired")]
public class WebTimer : System.Web.UI.WebControls.WebControl, 
                                          IPostBackDataHandler

As you can see we did the change to the default property and there are some other changes as well. First the DefaultEvent, that way the control will know which event is the default and when we double click it in the design time it will open up the code behind, just the way we like it to do. We didn't declare the event yet but we will soon. The second thing we see here is the fact we inherit the IPostBackDataHandler interface, that's because we want to be able to post back the client side script and fire an event when that happens on the server side.

C#
public event EventHandler IntervalExpired;

This is the line where we declare the event, I called it IntervalExpired.

C#
private int interval=5000; 
<Bindable(true)>
[Category("Data")]
[DefaultValue(5000)]
[Description("the interval set for the postback in millisec")]
public int Interval
{
    get
    {
        return interval;
    }
    set
    {
        interval = value;
    }
}

This is the property declaration, plain and simple, Interval.

The next section is the Helper function, but since I don't use it, I won't go into it too much. At first I thought I'll need the form name that the control is in, but then I found out I don't, anyway the function just stayed there and if you are going to develop a control yourselves this function is almost essential. It loops through the controls in the page and finds out the one that is holding the control.

The real juice is in the last part, control events. Here we have the events that are fired and the stuff that happens when they do.

C#
protected override void Render(HtmlTextWriter output)
{
output.Write(this.ID);
}

This is the Render override, I really don't use it much as you can see, I just put in the control ID so you can see it in the design view. And this control has no visible GUI anyway on the client side. If you look back at the code:

C#
public WebTimer()
{
    this.Visible=false;

}

This is the constructor that makes the control invisible at runtime. (if anyone knows a better way to do this, please tell me).

C#
protected override void OnInit(EventArgs e) 
{ 
    base.OnInit(e); 
    // verify that the page object is valid 
    if (Page != null) 
    { 
        Page.RegisterRequiresPostBack(this); 
        // Verify that the startup script isn't
        // already registered (postbacks) 
        if(!Page.IsStartupScriptRegistered(this.ID+"_TCDooM")) 
        { 
            // Form the script to be registered at client side. 
            StringBuilder sb = new StringBuilder(); 
            string sFormName = GetFormName(this.Page); 
            sb.Append("<script language="'javascript'">\n"); 
            sb.Append("{\n"); 
            sb.Append("window.setTimeout(\""+
               Page.GetPostBackClientEvent(this,"")+
               "\","+interval+");\n"); 
            sb.Append("}\n"); 
            sb.Append("</script>\n"); 
            // Register the startup script 
            Page.RegisterStartupScript(this.ID+"_TCDooM", 
                                            sb.ToString()); 
        } 
    } 
}

This code is really all the control, on the init event we add the code that will render the client side script. First we run the OnInit event we inherit, then we check to see if the page is valid - if (Page != null), then we register this control as a control that requires post back, this will in fact generate the code needed for post back at the client side (if you view the source of the HTML generated you'll see a function in the script named __postback, that's it!). After that, we check to see if we already have that script block registered so we won't have it twice or more. Then the string builder is there to help us out building our string, nothing fancy, just a script tag and the SetTimeout function. The last line is the one that registers the script to the page so it will be rendered; the first parameter is the name we give it, the same name we checked to see if it already exists.

C#
bool IPostBackDataHandler.LoadPostData(string postDataKey, 
                                      NameValueCollection postCollection)
{
    if(postDataKey==this.ID)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void IPostBackDataHandler.RaisePostDataChangedEvent()
{
    IntervalExpired(this, EventArgs.Empty);

Those two methods are the implementation of the IPostBackDataHandler interface. The first one is called whenever a post back is made for this control and if it returns true the other one is fired. So when ever a postback is made with the id of the control we fire up the RaisePostDataChangedEvent method and there we fire up the IntervalExpired event. This event is the one you'll write code for in the code behind.

Conclusion

This is a small and easy to make control and I think that everyone who has a bit of development background can understand it. Just read the code and see what it does, some of the code is just copy-paste off the Internet. Just so you'll know, I wrote this in 3-4 hours; since I never wrote a server control before, so can you, I never knew how to register a client side code and how to make an event the default event and so on... So what I'm saying is - it's easy.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Team Leader Qualisystems
Israel Israel
Hell, i'm a team leader at Qualisystems, a software company.
all is well, thanks for asking Smile | :)

TCDooMand the masters of the universe

Comments and Discussions

 
Generalsettimeout Pin
Ajay Kale New27-Sep-10 18:44
Ajay Kale New27-Sep-10 18:44 
GeneralRegading the timeout Pin
Member 6381174-Feb-09 23:34
Member 6381174-Feb-09 23:34 
Generaltry this... Pin
Prem Rajadattan30-Sep-08 5:32
Prem Rajadattan30-Sep-08 5:32 
GeneralIt works Pin
dcodco18-Jun-07 19:22
dcodco18-Jun-07 19:22 
QuestionEnhancement to the control Pin
pintea19-Jan-07 4:43
pintea19-Jan-07 4:43 
GeneralRefreshing Adrotator control Pin
Md Mustafa22-Sep-06 0:28
Md Mustafa22-Sep-06 0:28 
GeneralTimer control in the ASP.NET world Pin
OmegaCD8-Jul-06 21:35
OmegaCD8-Jul-06 21:35 
GeneralI have to say!!! Pin
HowerS2-Dec-05 10:03
HowerS2-Dec-05 10:03 
GeneralInterval changes by button-click Pin
RAKok11-May-05 22:05
RAKok11-May-05 22:05 
QuestionHow to refresh only control instead of entire WebForm Pin
12-Mar-05 9:58
suss12-Mar-05 9:58 
GeneralWorks for Me Pin
flaunt8-Mar-05 9:01
flaunt8-Mar-05 9:01 
QuestionBuilt-in timer control??? Pin
bbehm20-Jun-04 7:29
professionalbbehm20-Jun-04 7:29 
Is it possible to use the timer component that comes with Visual Studio to achieve the same results?
AnswerRe: Built-in timer control??? Pin
TCDooM20-Jun-04 20:00
TCDooM20-Jun-04 20:00 
GeneralCorrected ASP.NET Web Timer - timeout can be set in run-time too Pin
soniko16-Apr-04 11:28
soniko16-Apr-04 11:28 
Questionenabled/disabled ?? Pin
mhunt13-Apr-04 10:29
mhunt13-Apr-04 10:29 
Generalgreat !!! Pin
lije1329-Aug-03 4:13
lije1329-Aug-03 4:13 
GeneralCan't set the Interval at runtime Pin
Member 18325514-May-03 22:48
Member 18325514-May-03 22:48 
GeneralRe: Can't set the Interval at runtime Pin
CodeFriendly28-Jul-03 8:19
CodeFriendly28-Jul-03 8:19 
GeneralRe: Can't set the Interval at runtime - seems to be fixed Pin
soniko28-Mar-04 3:37
soniko28-Mar-04 3:37 
GeneralError when running control Pin
MikeMike6613-May-03 2:08
MikeMike6613-May-03 2:08 
GeneralRe: Error when running control Pin
soniko18-Mar-04 8:18
soniko18-Mar-04 8:18 
GeneralRe: Error when running control Pin
granadaCoder14-Feb-07 5:35
granadaCoder14-Feb-07 5:35 
Generalsimpler way to do the same Pin
Paul Horstink5-May-03 19:51
Paul Horstink5-May-03 19:51 
GeneralRe: simpler way to do the same Pin
TCDooM5-May-03 20:04
TCDooM5-May-03 20:04 
GeneralRe: simpler way to do the same Pin
Paul Horstink5-May-03 21:32
Paul Horstink5-May-03 21:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.