vb.net 单选按钮检查更改甚至不触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37244546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Radio Button Check Changed even not firing
提问by Knowledge2Share
I've 2 radio buttons in a user control and control is registered to the page. When i click on the radio button, the event(CheckChanged) is not firing.
我在用户控件中有 2 个单选按钮,并且控件已注册到页面。当我点击单选按钮时,事件(CheckChanged)没有触发。
<asp:View ID="viewfirst" runat="server">
<asp:UpdatePanel ID="updatepanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:RadioButton ID="radio1" Text="Yes" Enabled="true" runat="server" />
<asp:RadioButton ID="radio2" Text="No" Enabled="true" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:View>
Below is code in behind file of the control.
Protected Sub radio1_CheckedChanged(sender As Object, e As EventArgs) Handles radio1.CheckedChanged
//
//
End Sub
It seems everything looks good, but something is wrong. Can you please let me know.
看起来一切都很好,但有些地方不对劲。你能告诉我吗。
回答by jelliaes
Set autopostback=truefor checkbox - This will trigger the event
UpdateMode=conditionaland ChildrenAsTriggers=falsefor the UpdatePanel - so when you trigger checkbox, it won't trigger full postback
为复选框设置autopostback=true- 这将触发事件
UpdateMode=conditional和ChildrenAsTriggers=false对于 UpdatePanel - 所以当你触发复选框时,它不会触发完整的回发
回答by Abigail Mak
You have to set the event handler of each radio button to OnCheckChangedand put them in the same RadioGroup(GroupName) so they do not fire together. Remember to set AutoPostBack = truefor each radio button. Set only oneradioButton as checked = true. I can see your code has both checked. Something like this...
您必须将每个单选按钮的事件处理程序设置为OnCheckChanged并将它们放在同一个RadioGroup(GroupName) 中,这样它们就不会一起触发。请记住为每个单选按钮设置AutoPostBack = true。仅将一个单选按钮设置为已选中 = true。我可以看到您的代码都已检查。像这样的东西...
The aspx page:
aspx页面:
<asp:RadioButton id="radioButton1" runat="server" GroupName="btnGrp" Text="Button 1" AutoPostBack="true" Checked="true" OnCheckedChanged="radioButton1_CheckedChanged"></asp:RadioButton>
<asp:RadioButton id="radioButton2" runat="server" GroupName="btnGrp" Text="Button 2" AutoPostBack="true" OnCheckedChanged="radioButton2_CheckedChanged"></asp:RadioButton>
The code-behind (aspx.cs) page:
代码隐藏 (aspx.cs) 页面:
protected void radioButton1_CheckedChanged(object sender, EventArgs e)
{
// Your code here
}
protected void radioButton2_CheckedChanged(object sender, EventArgs e)
{
// Your code here
}
Hope this helps!
希望这可以帮助!

