How to find ListBox item by text in asp.net c#

Find ListBox Item By Text
ListBox is an ASP.NET list web server control. Developers can use the ListBox control to create a single or multi-item selection list control. We can set single selection or multi-selection mode using the ListBox SelectionMode property. ListBox Items property contains all ListItem objects.

ListBox can populate with items from a data source control. So a ListBox can exists many items. Each ListItem object has a Text and a Value property. Text property holds the text to display in ListBox and Value property text is associated with the ListItem object and is hidden in the web browser.

The .NET C# developers can find/search a ListItem object in ListBox using the ListItem Text property value. To find an item using its Text we need to use the Items FindByText(String) method. Items FindByText() method finds an item from ListBox using its Text property string.

The following C# example source code demonstrates to us how can we find a ListBox item by text in ASP.NET.
ListBoxFindItemByText.aspx

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

<!DOCTYPE html>

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to find item by text in ListBox, FindByText()</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Green">ListBox example: Find By Text</h2>
        <asp:Label 
             ID="Label1" 
             runat="server"
             Font-Bold="true"
             ForeColor="Red"
             Font-Size="Large"
             >
        </asp:Label>
        <br /><br />
        <asp:Label 
             ID="Label2" 
             runat="server" 
             Text="asp.net controls"
             Font-Bold="true"
             ForeColor="DarkGreen"
             >
        </asp:Label>
        <br />
        <asp:ListBox 
             ID="ListBox1"
             runat="server"
             BackColor="DarkGreen"
             ForeColor="FloralWhite"
             >
             <asp:ListItem>ListBox</asp:ListItem>
             <asp:ListItem>XmlDataSource</asp:ListItem>
             <asp:ListItem>SqlDataSource</asp:ListItem>
             <asp:ListItem>BulletedList</asp:ListItem>
             <asp:ListItem>RadioButton</asp:ListItem>
        </asp:ListBox>
        <br /><br />
        <asp:Label 
             ID="Label3" 
             runat="server"
             ForeColor="HotPink"
             Text="Item Text"
             >
        </asp:Label>
        <asp:TextBox 
             ID="TextBox1"
             runat="server"
             BackColor="HotPink"
             ForeColor="Snow"
             >
        </asp:TextBox>
        <br />
        <asp:Button 
             ID="Button1" 
             runat="server"
             Text="Find"
             Font-Bold="true"
             ForeColor="DarkGreen"
             OnClick="Button1_Click"
             />
    </div>
    </form>
</body>
</html>