|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis 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 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 codeOk, 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: [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 [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 public event EventHandler IntervalExpired;
This is the line where we declare the event, I called it private int interval=5000;
This is the property declaration, plain and simple, The next section is the 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. protected override void Render(HtmlTextWriter output)
{
output.Write(this.ID);
}
This is the 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). 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 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 ConclusionThis 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. | ||||||||||||||||||||