asp.net - How to find an item by text in a DropDownList

DropDownList.Items.FindByText(String) Method
DropDownList represent a control that allow the user to select a single item from a drop-down-list. .net developers can populate a dropdownlist control by items using data bind with a data source control. they also can specify dropdownlist items by placing ListItem objects between opening and closing dropdownlist tags using declarative syntax.

ListItem object have a Text property that get or set the text displayed in dropdownlist. and have a Value property that is associated with the ListItem. Value property value is hidden in web browser.

we can find a dropdownlist item by its Text property programmatically at run time. .net ListItemCollection.FindByText method search the item collection for a ListItem with a Text property that equals the specified text. this method is case-sensitive and culture-insensitive. null is returned if this method does not found an item.

the following asp.net c# example source code demonstrate us how can we find a list item from dropdownlist using specified item text.
DropDownListItemFindBytext.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    { 
       string searchString = TextBox1.Text.ToString();
       if (DropDownList1.Items.FindByText(searchString) != null)
       {
           Label1.Text = "Item Found: " + searchString;
       }
       else
       {
           Label1.Text ="Item not Found: " + searchString;
       }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to find item by text in DropDownList, FindByText()</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Green">DropDownList example: Find By Text</h2>
        <asp:Label 
             ID="Label1" 
             runat="server"
             Font-Bold="true"
             ForeColor="SaddleBrown"
             Font-Size="Large"
             >
        </asp:Label>
        <br /><br />
        <asp:Label 
             ID="Label2" 
             runat="server" 
             Text="asp.net controls"
             Font-Bold="true"
             ForeColor="DodgerBlue"
             >
        </asp:Label>
        <br />
        <asp:DropDownList 
             ID="DropDownList1"
             runat="server"
             BackColor="Crimson"
             ForeColor="FloralWhite"
             >
             <asp:ListItem>Repeater</asp:ListItem>
             <asp:ListItem>LoginName</asp:ListItem>
             <asp:ListItem>DataList</asp:ListItem>
             <asp:ListItem>LoginView</asp:ListItem>
             <asp:ListItem>LinqDataSource</asp:ListItem>
        </asp:DropDownList>
        <br /><br />
        <asp:Label 
             ID="Label3" 
             runat="server"
             ForeColor="SaddleBrown"
             Text="Item Text"
             >
        </asp:Label>
        <asp:TextBox 
             ID="TextBox1"
             runat="server"
             BackColor="SaddleBrown"
             ForeColor="Snow"
             >
        </asp:TextBox>
        <br />
        <asp:Button 
             ID="Button1" 
             runat="server"
             Text="Find In DropDownList"
             Font-Bold="true"
             ForeColor="SeaGreen"
             OnClick="Button1_Click"
             />
    </div>
    </form>
</body>
</html>
More asp.net examples