Search code examples
asp.netradiobuttonlist

How to set value to RadioButtonList Item in ASP.NET


I have a RadioButtonList that includes 2 values - "Yes" and "no". I want that everytime a user gets into the form page, the value will be set as the one he chose when he filled the form in the first time. I have no problem doing it with the TextBoxes, only with the RadioButtonList. Here are the codes:

        <asp:RadioButtonList ID="RadioButtonList1" runat="server">
        <asp:ListItem Value="Yes"></asp:ListItem>
        <asp:ListItem Value="No"></asp:ListItem>
        </asp:RadioButtonList>

the Code behind: (the 'if' checks in the DB if the user chose the "No" Radio button when he first filled the form, the ??? is what I need from you :))

            if ((String)cmd3.ExecuteScalar() == "No")
        {
            ???
        }

I tried RadioButtonList1.Items.FindByValue("No").Selected = true; but it did'nt work.

Let me know if I wasn't clear, and Thanks. Idan.


Solution

  • how about:

     if ((String)cmd3.ExecuteScalar() == "No")
     {
        RadioButtonList1.SelectedIndex  = 1;
     }
     else
     {
        RadioButtonList1.SelectedIndex  = 0;
     }