vb.net单选按钮列表检查是否已选中

时间:2012-05-31 20:11:49

标签: asp.net vb.net

我有以下内容:

    <asp:RadioButtonList ID="rbIsRep" runat="server"  RepeatDirection="horizontal" >
        <asp:ListItem Value="1" >Yes</asp:ListItem>
        <asp:ListItem Value="0" >No</asp:ListItem>
    </asp:RadioButtonList>

如何检查radiobuttonlist是否有任何选定的项目?

2 个答案:

答案 0 :(得分:6)

使用SelectedIndex属性检查是否选择了任何内容,并使用SelectedItem属性获取所选项目的文本:

If rbIsRep.SelectedIndex > - 1 Then
    Dim selectedItemsText = "You selected: " & rbIsRep.SelectedItem.Text
End If

您可以通过编程方式更改选择,例如使用SelectedValue属性。

rbIsRep.SelectedValue = "0"

或以声明方式来自aspx

<asp:RadioButtonList ID="rbIsRep" runat="server"  RepeatDirection="horizontal" >
    <asp:ListItem Value="1" >Yes</asp:ListItem>
    <asp:ListItem Selected="True" Value="0" >No</asp:ListItem>
</asp:RadioButtonList>

答案 1 :(得分:0)

一种方法:

var hasSelection = yourRadioButtonList.SelectedIndex != -1;

对于VB.NET:

Dim hasSelection = yourRadioButtonList.SelectedIndex <> -1
相关问题