我有两个单选按钮都设置为更新面板的异步触发器,问题是第一次点击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中设置了一个断点,它只在第一次出现。
有什么想法吗?
答案 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>
希望这有帮助。