RadioButton CheckedChanged event doesn't fire if default value is checked

johna by | March 27, 2012 | ASP.NET Web Forms Web Development

Whether a bug or by design, if you have a radio button with autopostback set to true and you set the default value to true, the CheckedChanged event will not fire if the RadioButton is the trigger for an AJAX UpdatePanel but outside of the panel.

This can be reproduced using the following sample code:

Markup:
<asp:ScriptManager ID="ScriptManager" runat="server"></asp:ScriptManager>
<p><asp:RadioButton ID="rdoOne" runat="server" AutoPostBack="true" Checked="true" GroupName="Radios" OnCheckedChanged="rdoOne_CheckedChanged" Text="One" /></p>
<p><asp:RadioButton ID="rdoTwo" runat="server" AutoPostBack="true" GroupName="Radios" OnCheckedChanged="rdoTwo_CheckedChanged" Text="Two" /></p>
<asp:UpdatePanel ID="panResult" runat="server">
<ContentTemplate>
<p><asp:Literal ID="litResult" runat="server"></asp:Literal></p>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="rdoOne" EventName="CheckedChanged" />
<asp:AsyncPostBackTrigger ControlID="rdoTwo" EventName="CheckedChanged" />
</Triggers>
</asp:UpdatePanel>

Code behind:
protected void rdoOne_CheckedChanged(object sender, EventArgs e)
{
litResult.Text = "One " + DateTime.Now;
}
protected void rdoTwo_CheckedChanged(object sender, EventArgs e)
{
litResult.Text = "Two " + DateTime.Now;
}

Notice that checking rdoOne has no result.

There are a couple of simple solutions to this problem.

1. Do not set checked="true" in the markup, instead do it in the code behind like this:
rdoOne.InputAttributes["checked"] = "true"

2. Add your own onclick event to rdoOne, like this:
rdoOne.Attributes.Add("onclick", "javascript:setTimeout('__doPostBack(\'" + rdoOne.ClientID.Replace("_", "$") + "\',\'\')', 0)");


Update: The real cure for this is to update the UpdatePanel containing the RadioButton, RadioButtonList, CheckBox or CheckBoxList (and add an UpdatePanel if you don't already have one) as ASP.NET is perfectly capable of sorting this out on its own if used as intended. I don't believe it's a bug, just the fact that sometimes ASP.NET controls need to be updated when certain things change.

Update: The real cure for this is to update the UpdatePanel containing the RadioButton, RadioButtonList, CheckBox or CheckBoxList (and add an UpdatePanel if you don't already have one) as ASP.NET is perfectly capable of sorting this out on its own if used as intended. I don't believe it's a bug, just the fact that sometimes ASP.NET controls need to be updated when certain things change.

Related Posts

Web Development

How to set up a debugging using the Turnkey Linux LAMP stack and VS Code

by johna | December 19, 2023
The second part in my guide to setting up a website and database using the Turnkey Linux LAMP stack.

Website Hosting Web Development

How to set up a website and database using the Turnkey Linux LAMP stack

by johna | November 18, 2023
If you need to host your own website for the purposes of web development, Turnkey Linux LAMP Stack is an easy to install all-in-one solution that you can set up on a spare computer or a VM (Virtual Machine).

Web Development

Intermittent "Unable to read data from the transport connection: net_io_connectionclosed" errors

by johna | May 6, 2020
If you are having intermittent problems sending email in .NET using System.Net.Mail consider switching libraries.

Comments

CoOl

by CoOl | July 3, 2013

Great Help!!! Thanks.

Reply

Leave a Comment

About

...random postings about web development and programming, Internet, computers and electronics topics.

I recommend ASPnix for web hosting and Crazy Domains for domain registration.

Subscribe

Get the latest posts delivered to your inbox.