NullifyNetwork

The blog and home page of Simon Soanes
Skip to content
[ Log On ]

Whilst I wait for someone to get back to me about something, I figured I'd quickly post about something that initially confused me about server controls on ASP.NET so that there's at least SOME new content!

ASP.NET server controls are simple components that run on the server and allow you to refactor a quantity of your sites code (such as a menubar) into a seperate module that is both reusable like a user control, and dynamic.  A server control can run code and also allows you to drag and drop it onto your pages.  A server control also has the ability to render itself at design time, so no more silly grey boxes - but rather a WYSIWYG situation (once it's built)!

So, once you've created a server control using VS.NET 2003 (or your choice of IDE!) you can easily add to the render override with something simple:

output.Write("<div class=\"mycssclass\">Hi there!</div>");

Any HTML will work, and all is fine if you just want to insert static HTML.

But what if you want to use the server control as if it were an ASP.NET application - or winforms control?

You know, so you can click a link or a button and it'll do 'something' and remember that state.

To do this you will need an event handler attached to a control.  So you create an instance of a control and delegate its event to the handler!  In winforms this is extremely easy, but in ASP.NET making it work right requires you set it all up early enough in your code!  This is the gotcha that had me stuck for a couple of hours when I first learnt it.  I did it in the render override.

Instead, change your render override to read as follows:

protected override void Render(HtmlTextWriter output)
{
   output.Write("<div class=\"myCSSClass\">");
   this
.RenderContents(output);
   output.Write("</div>");
}

This will allow you to render the controls at the right time and yet create them early enough - which will let ASP.NET create the eventhandler and wire it up for you.

Now, to create the controls that will be rendered you need to create a new override:

public LinkButton l;

protected override void OnInit(EventArgs e)
{
   base
.OnInit (e);
   l = new
LinkButton();
   l.Text = "Click me!  I'm a link that triggers an event on the server!";
   l.Enabled = true
;
   EventArgs args = new
EventArgs();
   l.Click += new
EventHandler(this.l_Click);
   this
.Controls.Add(l);
   Literal br = new
Literal();
   br.Text = "<br />";
   this
.Controls.Add(br);
}

This will create two controls - which will be rendered in the render method.  You can now create the event handler and any code you place in it will work!  If you had created the controls in the render method itself you would have found that although they posted back to the server, they didn't call the event handler.

(Note: += means "add one of" and "base" is the WebControl you are inheriting from when creating a server control.)

A quick example event handler for those who've never done one before:

void l_Click(object sender, EventArgs e)
{
   l.Text = "Thanks!";
}

Permalink