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

John Avis by | March 27, 2012 | ASP.NET Web Forms

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

ASP.NET Web Forms Bootstrap

More on Bootstrap 4 modals in ASP.NET Web Forms applications using UpdatePanels

by John Avis | April 9, 2019

In this, my third post on this topic, I go over the methods I am using now and the problems I have encountered and overcome.


ASP.NET Web Forms Classic ASP

Custom error pages for ASP.NET Web Forms and Classic ASP in IIS 7 and 8.5

by John Avis | February 20, 2019

I was recently doing some work on a website which has a mixture of older Classic ASP pages and ASP.NET Web Forms pages and ran into problems with custom error pages.


ASP.NET Web Forms

Nicer localised UpdateProgress’ for your ASP.NET pages with multiple UpdatePanels

by John Avis | February 9, 2018

On an ASP.NET Web Forms page with multiple UpdatePanels it’s nice to give the user some feedback when one of the panels is being updated.

Comments

CoOl

by CoOl | July 3, 2013

Great Help!!! Thanks.

Reply

Leave a Comment
Tags
ASP.NET Html Forms ASP.NET MVC ASP.NET Web Forms ASP.NET Web Pages Bootstrap C# Classic ASP Cool Websites Databases eBay and PayPal Electrical Repairs General Hardware HTML/CSS Jquery/Javascript Media Center Mobile Phones Responsive Web Design Reviews SEO and Social Networking Web Design Web Development Web Security web+db Website Hosting Windows XP Youtube

About me

...mostly about web development and programming, with a little bit of anything else related to the Internet, computers and technology.

Subscribe

Get the latest posts delivered to your inbox.