Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET 4.0 Radio button checked changed event fires only once

Tags:

c#

asp.net

I have two radio buttons both set as async triggers for an update panel and problem is that first time one is clicked the CheckedChanged event fires but then no matter which radio button is clicked the event never fires again.

Markup:

<asp:RadioButton ID="rdoDeliveryBilling" runat="server" Checked="true" GroupName="DeliveryAddress" Text="Deliver to this address" AutoPostBack="true" OnCheckedChanged="rdoDelivery_CheckedChanged" />
<asp:RadioButton ID="rdoDeliveryShipping" runat="server" GroupName="DeliveryAddress" Text="Deliver to a different address" AutoPostBack="true" OnCheckedChanged="rdoDelivery_CheckedChanged" />
<asp:UpdatePanel ID="panDeliveryAddress" runat="server">
<ContentTemplate>
    ...delivery details form controls and validators goes here...
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="rdoDeliveryBilling" EventName="CheckedChanged" />
<asp:AsyncPostBackTrigger ControlID="rdoDeliveryShipping" EventName="CheckedChanged" />
</Triggers>
</asp:UpdatePanel>

Code:

protected void rdoDelivery_CheckedChanged(object sender, EventArgs e)
{
    ...only code that enables/disables the delivery form controls and validators goes here...
}

I have set a breakpoint inside rdoDelivery_CheckedChanged and it only hits the first time.

Any ideas?

like image 472
johna Avatar asked Dec 06 '22 18:12

johna


1 Answers

Looking at the source (in the browser), ASP.NET is only generating a post back function __doPostBack for the RadioButton controls which can possibly postback.

The first RadioButton control cannot postback (because it is already checked), and as such the __doPostBack is not generated.

A work around is to add the two RadioButton controls to another UpdatePanel, setting the UpdateMode to Always. This will cause the RadioButtons to be updated (whenever they trigger the other UpdatePanel) adding the __doPostBack function to the deselected RadioButton.

Example

<asp:UpdatePanel ID="UpdatePanelCheckBoxes" runat="server" UpdateMode="Always">
    <ContentTemplate>
        <asp:RadioButton ID="rdoDeliveryBilling" runat="server" Checked="true" GroupName="DeliveryAddress" Text="Deliver to this address" AutoPostBack="true" OnCheckedChanged="rdoDelivery_CheckedChanged" />
        <asp:RadioButton ID="rdoDeliveryShipping" runat="server" GroupName="DeliveryAddress" Text="Deliver to a different address" AutoPostBack="true" OnCheckedChanged="rdoDelivery_CheckedChanged" />            
    </ContentTemplate>
</asp:UpdatePanel>

Hope this helps.

like image 87
jdavies Avatar answered Jan 19 '23 09:01

jdavies