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

John Avis 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

Computers & Internet Web Development Website Hosting

500 Internal Server Error after migrating from IIS 7.5 to IIS 10

by John Avis | November 4, 2019

As support ends for Microsoft Windows Server 2008 I have recently gone through migrating some websites to a new server running Windows Server 2016 and IIS 10 but some of the websites did not work.


Jquery/Javascript Web Development

tagInput: A simple jQuery plugin for tag entry using Bootstrap 4

by John Avis | October 15, 2019

For a website project I needed a way to enter multiple tags. I just wanted something simple that I could easily modify to suit my own needs, so I wrote my own.


ASP.NET Web Forms Web Development

ASP.NET bug with RadioButton GroupName in Repeater

by John Avis | September 1, 2019

I rediscovered a bug in ASP.NET that affects RadioButtons inside repeaters. Here is my solution to the problem.

Comments

CoOl

by CoOl | July 3, 2013

Great Help!!! Thanks.

Reply

Leave a Comment

Tags

About me

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

Subscribe

Get the latest posts delivered to your inbox.