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

Nat*_*Pet 3 vb.net asp.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>
Run Code Online (Sandbox Code Playgroud)

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

Tim*_*ter 6

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

If rbIsRep.SelectedIndex > - 1 Then
    Dim selectedItemsText = "You selected: " & rbIsRep.SelectedItem.Text
End If
Run Code Online (Sandbox Code Playgroud)

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

rbIsRep.SelectedValue = "0"
Run Code Online (Sandbox Code Playgroud)

或者从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>
Run Code Online (Sandbox Code Playgroud)