65.9K
CodeProject is changing. Read more.
Home

RadioButtonList

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Oct 11, 2013

CPOL
viewsIcon

11363

A RadioButtonList can be used to select 1 option within several options. The item "Pizza with egg" is selected by default.    <asp:RadioButtonList

A RadioButtonList can be used to select 1 option within several options. The item "Pizza with egg" is selected by default.

    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                <asp:ListItem Selected="True">Pizza with egg</asp:ListItem>
<asp:ListItem>Pizza with tuna </asp:ListItem>
<asp:ListItem>Pizza with ham</asp:ListItem>

   </asp:RadioButtonList>

 

Set selected item from Database

If you load Data from a database you sometimes want to select a certain item based on the value in the DB.

You can do so by doing the following:

    string pizzaOptionFromDB = someCode.getPizzaFromDB();  // DB returns  "Pizza with tuna" 
    ListItem li = RadioButtonList1.Items.FindByValue(pizzaOptionFromDB);
    if (li != null)
        {
            li.Selected = true;
        }

This selects the ListItem with the string "Pizza with tuna".