asp.net - How to find an item by value in a CheckBoxList

Find CheckBoxList Item By Value
asp.net checkboxlist web server control allow developers to render a multi selection checkbox group in web browser. checkboxlist display list items. an Items collection hold all the ListItem objects in checkboxlist control. each ListItem object can have a Text and a Value property. ListItem Text property text display in web browser and Value property text is associated with ListItem object as additional information.

sometimes a checkboxlist contains many items and we need to find specific item from the list. the following example c# code demonstrate us how can we find a list item from checkboxlist by item value programmatically at run time.

ListItemCollection class FindByValue method search the collection for a item that contains specified value. FindByValue method require a string parameter that contains the value of specified item.
CheckBoxListItemFindByValue.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    { 
       string searchText = TextBox1.Text.ToString();
       if (CheckBoxList1.Items.FindByValue(searchText) != null)
       {
           Label1.Text = "Item Found, Value: " + searchText;
           CheckBoxList1.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 CheckBoxList, FindByValue()</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">CheckBoxList example: Find By Value</h2>
        <asp:Label 
             ID="Label1" 
             runat="server"
             Font-Bold="true"
             ForeColor="Teal"
             Font-Size="Large"
             >
        </asp:Label>
        <br /><br />
        <asp:Label 
             ID="Label2" 
             runat="server" 
             Text="Color List"
             Font-Bold="true"
             ForeColor="DarkGreen"
             >
        </asp:Label>
        <br />
        <asp:CheckBoxList 
             ID="CheckBoxList1"
             runat="server"
             BackColor="DarkGreen"
             ForeColor="FloralWhite"
             >
             <asp:ListItem Value="1">MediumOrchid</asp:ListItem>
             <asp:ListItem Value="2">MediumPurple</asp:ListItem>
             <asp:ListItem Value="3">MidnightBlue</asp:ListItem>
             <asp:ListItem Value="4">MintCream</asp:ListItem>
             <asp:ListItem Value="5">Moccasin</asp:ListItem>
             <asp:ListItem Value="6">NavajoWhite</asp:ListItem>
        </asp:CheckBoxList>
        <br /><br />
        <asp:Label 
             ID="Label3" 
             runat="server"
             ForeColor="SeaGreen"
             Text="Item Value"
             >
        </asp:Label>
        <asp:TextBox 
             ID="TextBox1"
             runat="server"
             BackColor="SeaGreen"
             ForeColor="Snow"
             >
        </asp:TextBox>
        <br />
        <asp:Button 
             ID="Button1" 
             runat="server"
             Text="Find Item"
             Font-Bold="true"
             ForeColor="SeaGreen"
             OnClick="Button1_Click"
             />
    </div>
    </form>
</body>
</html>
More asp.net examples