Search code examples
c#javascriptasp.netradiobuttonlist

RadioButtonList vs CheckBox


I am using ASP.NET to create a web application form. Previously I was using checkboxes to accept a user input with 3 choices: add, edit, delete.

Used some server side and client side code to toggle between the 3 choices and store final answer from the user.

Then I came across "Radio Button List". So this is my list.

        <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>

This is so much more compact! than toggling between check box options.

However can anyone show me how to accept and store the client response on the server side?

Before with Checkboxes it was like:-

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

I am not sure how to access individual items on the radiobutton List. Can anyone help me out?


Solution

  • It's probably easiest to use the SelectedIndex, SelectedItem and SelectedValue properties of the RadioButtonList.

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