asp.net - How to find an item by value from a RadioButtonList

Find an item from RadioButtonList by item value
The following asp.net c# example code demonstrate us how can we find an item from RadioButtonList control by item's 'Value' property value. RadioButtonList is an asp.net list web server control. RadioButtonList contains one or more ListItem objects. Each ListItem object represent an item in RadioButtonList.

A ListItem object have a 'Text' property and optionally a 'Value' property. We can search through the RadioButtonList control for an item by its 'Value' property value. RadioButtonlist all items exists in an items collection. So we can manage RadioButtonList items by .net Collection<T> Class methods and properties.

Collection<T> Class FindByValue() method allow us to search through the RadioButtonList items collection by item's 'Value' property value. If the search operation find any item then the method return specified ListItem object; otherwise it returns null.

In this example code we search through the RadioButtonList items collection by item's value and if we find the specified item then we set this ListItem Selected property value to true. If we does not find any matches then a Label control display that there is no item with the specified value.
RadioButtonListItemFindByValue.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    { 
       string searchText = TextBox1.Text.ToString();
       if (RadioButtonList1.Items.FindByValue(searchText) != null)
       {
           Label1.Text = "Item Found and Select, Value: " + searchText;
           RadioButtonList1.Items.FindByValue(searchText).Selected = true;
       }
       else
       {
           Label1.Text = "Item not Found, Value: " + searchText;
       }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to find list item by value in RadioButtonList, FindByValue()</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Green">RadioButtonList example: Find By Value</h2>
        <asp:Label 
             ID="Label1" 
             runat="server"
             Font-Bold="true"
             ForeColor="DodgerBlue"
             Font-Size="Large"
             >
        </asp:Label>
        <br /><br />
        <asp:Label 
             ID="Label2" 
             runat="server" 
             Text="Color List"
             Font-Bold="true"
             ForeColor="Crimson"
             >
        </asp:Label>
        <br />
        <asp:RadioButtonList 
             ID="RadioButtonList1"
             runat="server"
             BackColor="Crimson"
             ForeColor="AliceBlue"
             >
             <asp:ListItem Value="1">Plum</asp:ListItem>
             <asp:ListItem Value="2">PowderBlue</asp:ListItem>
             <asp:ListItem Value="3">PaleGoldenRod</asp:ListItem>
             <asp:ListItem Value="4">PaleTurquoise</asp:ListItem>
             <asp:ListItem Value="5">Navy</asp:ListItem>
        </asp:RadioButtonList>
        <br /><br />
        <asp:Label 
             ID="Label3" 
             runat="server"
             ForeColor="Crimson"
             Text="Item Value"
             >
        </asp:Label>
        <asp:TextBox 
             ID="TextBox1"
             runat="server"
             BackColor="Crimson"
             ForeColor="Snow"
             >
        </asp:TextBox>
        <br />
        <asp:Button 
             ID="Button1" 
             runat="server"
             Text="Find Item"
             Font-Bold="true"
             ForeColor="Crimson"
             OnClick="Button1_Click"
             />
    </div>
    </form>
</body>
</html>
More asp.net examples