ASP.NET 4.0单选按钮选中已更改的事件仅触发一次

时间:2011-11-08 12:24:31

标签: c# asp.net

我有两个单选按钮都设置为更新面板的异步触发器,问题是第一次点击CheckedChanged事件时会触发,但无论点击哪个单选按钮,事件都不会再次触发。

标记:

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

代码:

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

我在rdoDelivery_CheckedChanged中设置了一个断点,它只在第一次出现。

有什么想法吗?

1 个答案:

答案 0 :(得分:13)

查看源代码(在浏览器中),ASP.NET仅为可能回发的__doPostBack控件生成回发函数RadioButton

第一个RadioButton控件无法回发(因为它已经被检查过),因此不会生成__doPostBack

解决方法是将两个RadioButton控件添加到另一个UpdatePanel,将UpdateMode设置为Always。这将导致RadioButtons被更新(每当他们触发另一个UpdatePanel时)将__doPostBack函数添加到取消选择的RadioButton

示例

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

希望这有帮助。