单选按钮列表与复选框

RadioButtonList vs CheckBox

本文关键字:复选框 列表 单选按钮      更新时间:2023-09-26

我使用ASP.NET创建web应用程序表单。以前我使用复选框接受用户输入,有3个选项:添加、编辑、删除。

使用一些服务器端和客户端代码在3个选项之间切换,并存储用户的最终答案。

然后我看到了"单选按钮列表"。这是我的清单。

        <asp:RadioButtonList ID="RadioButtonList1" runat="server">
            <asp:ListItem>Create a new enrollment.</asp:ListItem>
            <asp:ListItem>Change my enrollment information.</asp:ListItem>
            <asp:ListItem>Delete my account information.</asp:ListItem>
        </asp:RadioButtonList>

这是如此紧凑!而不是在复选框选项之间切换。

但是,有人能告诉我如何在服务器端接受和存储客户端响应吗?

在使用复选框之前,它就像:-

if(CheckBox1.Checked){'do something';} 

我不知道如何访问单选按钮列表中的各个项目。有人能帮我吗?

使用RadioButtonListSelectedIndexSelectedItemSelectedValue属性可能最简单。

switch(RadioButtonList1.SelectedValue)
{
    case "foo":
        // Do your stuff
        break;
}
// or check it in some if's
if(RadioButtonList1.SelectedValue == "foo")
{
    // Do your stuff
}