Archive for the AJAX Category

Missing / (in)action

Posted in AJAX, ASP / ASP.Net on July 19, 2008 by Si

Despite my recent lack of postings I am still alive!! 🙂

And that’s not the only reason for the title of this post. I have a monster in the making. Not a good monster mind, a real ugly monster. This monster is an ASP.NET user control. What started out as a simple task to develop a control that would allow users to specify some filters/parameters before generating a report seemed on paper, quite simple and feasible. However, adding in some nested user controls, several of my own hand-crafted javascript calendars (the built-in .NET calendar is really slow and I think painful to use), and some optional radio-buttons as a template column on a datagrid has given this a life of its own. A whole screen full of viewstate is really not the way forward!!!

Anyway, the other reason for this post is to spread/share the knowledge about radio buttons, AJAX and postback. In my monster control, I have a few radio buttons that let the user switch between filters, but obviously when the page loads I want a default filter to be shown. Correspondingly I want one of the radio buttons to be checked. “Easy!” you say, and, well, yes it is, and it might look something like:

<asp:RadioButton ID="RadioButton1" runat="server" GroupName="group1" Text="Option 1" Checked="true" OnCheckedChange="RadioButton1_CheckedChanged"/>

But is it quite that simple? When a radio button is selected, I want to do some stuff server-side and show a different filter through the magik of AJAX. The code above might work for buttons 2 and 3, but there is a distinct lack of action when button 1 is re-selected (after selecting 2 or 3). “Huh?” you say? The reason for this is the Checked="true" bit. When the page is rendered, this radio button doesn’t get the postback javascript associated with it’s onclick event, and so never triggers a postback. Solution? Wrap the radio buttons in an update panel so they get updated when AJAX does its thing.

Read this article for more info (it helped me win this particular battle with the monster):

I fight on….