【问题标题】:How to check one radio button out of two in vb.net如何在 vb.net 中检查两个单选按钮中的一个
【发布时间】:2012-01-28 07:37:06
【问题描述】:
假设我有 2 个单选按钮 r1 和 r2,这两个单选按钮都会询问您的性别,您可以是男性或女性。
所以我想要的是:如果用户检查了r1,但随后意识到她是女性,那么她想检查r2,因此r2 上的控件被选中,而r1 被取消选中。
<tr>
<td>
<asp:Label runat="server" text="Chooose Your Category" ID="lblcategory"></asp:Label>
</td>
<td>
<asp:RadioButton runat="server" Text="Male" ID="rbgold" />
</td>
<td>
<asp:RadioButton runat="server" Text="Female" ID="rbsilver" />
</td>
</tr>
接下来我应该怎么做才能只能选择一个?
提前致谢。
【问题讨论】:
标签:
vb.net
radio-button
checked
unchecked
【解决方案1】:
Raghav Chopra:这个问题不需要 RadioButtonList,您只需将单选按钮放在同一组中,然后一次只选择一个,您的问题也解决了
【解决方案2】:
我通过asp:RadioButtonList得到了答案
<tr>
<td>
<asp:Label runat="server" text="Chooose Your Category" ID="lblcategory">
</asp:Label>
</td>
<td class="style1">
<asp:RadioButtonList ID="rbgold" runat="server"
RepeatColumns="2"
Width="200px">
<asp:ListItem Text="Silver class" value="1" ></asp:ListItem>
<asp:ListItem Text="Gold class" value="2"></asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
【解决方案3】:
只要给这两个asp:RadioButtons 一样的GroupName
正如 MSDN 所指出的,
使用 GroupName 属性指定一组单选按钮以
创建一组互斥的控件。您可以使用 GroupName
从可用列表中只能选择一个时的属性
选项。
设置此属性时,指定组中只有一个 RadioButton
可以一次选择。
例子:
<tr>
<td>
<asp:Label runat="server" text="Chooose Your Category" ID="lblcategory">
</asp:Label>
</td>
<td>
<asp:RadioButton runat="server" Text="Male" ID="rbgold" GroupName="GenderGroup" />
</td>
<td>
<asp:RadioButton runat="server" Text="Female" ID="rbsilver" GroupName="GenderGroup" />
</td>
</tr>
【解决方案4】:
您需要将它们放在同一个组中,以便一次只能选择一个,例如:
<asp:RadioButton runat="server" Text="Male" ID="rbgold" GroupName="xyzzy" />
<asp:RadioButton runat="server" Text="Female" ID="rbsilver" GroupName="xyzzy" />