El botón de radio ASP.NET 4.0 marcado cambió el evento se activa solo una vez

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.

Margen:

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

Código:

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.

¿Alguna idea?

preguntado el 08 de noviembre de 11 a las 12:11

1 Respuestas

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

La primera 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, configurando el 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.

Ejemplo

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

Espero que esto ayude.

respondido 08 nov., 11:17

Thank you for helping me solve this problem. I may do as you suggested and put the radio buttons in an update panel, or I also found another solution to add the missing onclick: radiobutton.Attributes.Add("onclick", "javascript:setTimeout('_doPostBack(\'" & radiobutton.ClientID.Replace("", "$") & "\',\'\')', 0)") - johna

This solution didn't work for me. My radio buttons were already inside the update panel. I tried adding an update panel to the individual radio buttons (they were in different places on the page), but couldn't get it to work. But at least I know what the issue is now. Thanks for the helpful info. - Sal

I have two updatepanels with one RadioButton in each of the same Group Name and with UpdateMode="Always" I solved my problem. Thank you Men - Dumitru Chirutac

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.