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

ListBox Find 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 listbox SelectionMode property. listbox Items property contains of all ListItem objects.

listbox can populate with items from a datasource control. so a listbox can exists many items. each ListItem object have a Text and a Value property. Text property hold the text to display in listbox and Value property text is associated with ListItem object and it is hidden in web browser.

developers can find (search) a ListItem object in listbox using ListItem Text property value. to find a item using its Text we need to use Items.FindByText(String) method. Items.FindByText() method find a item from listbox using its Text property string.

the following c# example source code demonstrate us how can we find 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>
More asp.net examples