MS Ajax: RadioButton CheckedChanged not firing back with AJAX v1.0?
January 29, 2007 11 Comments
I saw a very interesting post on the Ajax forum. NXTwoThou had a problem with 2 radiobutton controls which must fire the OnCheckedChanged event. Here is the code.
- <script runat=”Server”>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
ddl_customers_specific.Items.Add(new ListItem(“Test”));
}
protected void rb_customers_all_CheckedChanged(object sender, EventArgs e)
{
ddl_customers_specific.BackColor = System.Drawing.Color.Red;
}
protected void rb_customers_specific_CheckedChanged(object sender, EventArgs e)
{
ddl_customers_specific.BackColor = System.Drawing.Color.Blue;
}
- </script>
<form id=”form1″ runat=”server”>
<asp:ScriptManager runat=”server” ID=”sm” />
- <asp:RadioButton ID=”rb_customers_all” runat=”server” Text=”All Customers” AutoPostBack=”true”
Checked=”true” GroupName=”g_Customers” OnCheckedChanged=”rb_customers_all_CheckedChanged” />
- <asp:RadioButton ID=”rb_customers_specific” runat=”server” Text=”Specific Customers” AutoPostBack=”true” GroupName=”g_Customers” OnCheckedChanged=”rb_customers_specific_CheckedChanged” />
- <asp:UpdatePanel ID=”up_customer” runat=”server” UpdateMode=”conditional”>
<ContentTemplate>
<asp:DropDownList ID=”ddl_customers_specific” runat=”server” />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID=”rb_customers_all” EventName=”CheckedChanged” />
<asp:AsyncPostBackTrigger ControlID=”rb_customers_specific” EventName=”CheckedChanged” />
</Triggers>
</asp:UpdatePanel>
</form>
The problem is that the radio button “rb_customers_all” has the property checked set to true. When this is set to true the event will never get fired. removing this property and adding the following code “rb_customers_all.InputAttributes[“checked”] = “true”;” will fix this problem.
Have fun!
Having a similar issue with radio button lists. The above solution modified for a list “rbList.Items[0].Attributes{“checked”] = “true” doesn’t quite work as it creates a span with a “checked” attribute?
Hi michel,
Can you email (dennisv@avanade.com your code to me then i can take a look at it this weekend.
Hi,
Thanks for the post. It helped me out of a situation.
But what is the difference between
rb_customers_all.InputAttributes[”checked”] = “true”;
and
rb_customers_all.Checked = true;
Thanks again.
Wish Id of seen this before I spent some serious time trying to figure out why the events stopped firing.
THANKS
Hi,
The InputAttributes[”checked”] approach helped me to get rid of the CheckChanged issue. It is working fine for me.
Thanks
Sandeep
But there is only one problem. Setting the checked attribute to true by using InputAttributes[”checked”] approach is not setting the btn.Checked to true.
Can any one tell me how to solve this.
Thanks
Sandeep
Hi Sandeep,
Do you mean that when you want to “check” if the checkbox is checked in the codebehind this isn’t available?
btn = a checkbox in your situation i believe
Regards
Thanks for help.
I think Sandeep is right.
If you want init the RadioButton with “RadioButton1.Checked = true” the “fix” don’t work.
Hi Thanks dennisv
That solved my problem. Not the easist thing to get working when you would expect the CheckedChanged to just work.
Thanks again
Andrew
Thanks! Wierd issue but your solution works!