Creating a Custom ASP.NET Control with Postback OnClick Event

Note: I’ve been meaning to post this article since September but it is getting too complicated. I’ll include some more info in a future article. Maybe I’ll even make this a series on making custom controls that postback.
I recently had to create a new ASP.NET control that contained a couple of images and a link. Sometimes I wanted it to postback when clicked, and sometimes I wanted it to run a script, and sometimes I wanted it to run a script and then postback. Sounds funny, but it is true. This was a toolbar button control I wanted to create and make it look like one of those Outlook buttons.

I’ll spare you most of the details in what I did, but in the end the way I designed the control I wanted to put an onclick event onto a span that wrapped the other parts of the control (images and text controls). There are two important methods for getting a postback:

  1. ClientScriptManager.GetPostBackClientHyperlink()
  2. ClientScriptManager.GetPostBackEventReference()

The ClientScriptManager is available as an instance property of the Page class, including Control.Page as Page.ClientScript.


This method returns javascript:__doPostBack() as a string. It is suitable for using in an anchor href or onclick event handler. Unfortunately it prepends the javascript: to the front of the string, which is kind of ugly, but also makes it hard to use if you wanted to do a more complicated function. Because I wanted to be able to call client-side scripts before doing the post-back it was unusable for me.


This method takes several parameters, including the names of an EventArgument, a ValidationGroup, and other goodies like tracking control focus between postbacks. The method returned is WebForm_DoPostBackWithOptions(), and also does not include the javascript: that GetPostBackClientHyperLink() gives you.

To make GetPostBackEventReference() work your control must implement the IPostBackEventHandler (and that is easy). Use IPostBackEventHandler.RaisePostBackEvent(string eventArgument) and switch on the eventArgument to define the server-side behaviour. For example, I passed “clicked” as the eventArgument to GetPostBackEventReference() so my RaisePostBackEvent() looks something like this:

void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
switch (eventArgument)


The FireOnClick() method fires the Click EventHandler.


Its easy to get the Client-Script to Postback using the GetPostBackEventReference() method.

In my next article I’ll describe how to execute your own Client-Side OnClick event before posting back to the server.

I’m a PC

In honour of my new computer, some of the quality issues in recent computers and the Apple Get A Mac Ads I had an idea for a new TV script.

So here goes:

Scene opens with PC and Mac standing there beside each other.

PC: I’m a PC.

Mac: And I’m a Mac.

PC: Hey Mac, you’ve got something on your face.

PC indicates a spot on his face.
Mac rubs his face. A huge scratch stretches across his face.

PC: Umm, Mac, you just scratched your face.

Mac: No I didn’t.

PC: Yes, I think you did.

PC’s leg falls off, then explodes.

Mac rolls his eyes at camera.

Mac (smug): I don’t explode, or crash.

Mac’s leg explodes, but he doesn’t notice. Mac closes eyes and lies down on the ground.

PC: Wake up Mac!

Mac opens eyes and stands up.

Mac: I wasn’t sleeping.

Steve Jobs comes on

Steve Jobs (smugly, but that is how he always talks): Macs are the best computers. Much better than PCs.

Mac starts to glow a bright orangy colour

Steve Jobs (still smugly): Macs are the best computers. Much better than PCs.

Steve Jobs’ hair catches fire.

Steve Jobs (even more smugly): I’ll bet you wish your hair was on fire.

Aaaaand Scene. Okay, that was fun. I’m not a PC zealot and I would love to own a Mac (though I’m waiting for their quality issues to pass before paying the premium prices Apple charges). I ordered a new PC the other day, and I got some really good quality parts. Windows XP is very stable, and I haven’t had a virus in the last 8 years or so. So I don’t know what the fuss is about.

I think Apple is spreading a lot of FUD with their ads, while at the same time taking a long time owning up to the problems that arise in their own hardware.

While Dell doesn’t make really awesome hardware, they at least own up to the issues in their hardware fairly quickly.

ASP.NET CheckBoxList DataBinding with Checked

So today I ran into an interesting issue with DataBinding to a CheckBoxList in ASP.NET 2.0; you cannot bind the checked state to the checked boxes. You can bind the Value and Text, but not the Checked state.

This leaves you with a few options for setting the checked state after databinding:

  1. Manually build the ListItem Collection for the CheckBoxList, setting the Value, Text and Selected values instead of databinding.
  2. Loop through the Items collection after databinding checking for each item.
  3. There is no number 3.

I decided to manually build a ListItem collection as follows:

foreach (DataRow dataRow in dataTable) {
string text =
string value = Convert.ToString(dataRow[COLUMN_VALUE]);
bool selected = Convert.ToBoolean(dataRow[COLUMN_SELECTED]);
ListItem newListItem = new ListItem();
newListItem.Text = text;
newListItem.Value = value;
newListItem.Selected = selected;

My New Blog

Hello everyone, this is my new weblog for my company’s site, The blog is humourously named Eric Will Code For Coffee! Yes, I laugh at my own cheesy jokes. 🙂

Coming soon I’ll write some posts about coding in ASP.NET, C#, and PHP as well as designing for ASP.NET with CSS – my favourite subject!