DropDownList.Items.FindByText(String) Method
DropDownList represents a control that allows the user to select a
single item from a drop-down list. The .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 has a Text property that gets or sets the text displayed in DropDownList. And have a Value property that is associated with the ListItem. Value property value is hidden in the web browser.
We can find a DropDownList item by its Text property programmatically at run time. The .NET ListItemCollection class’s FindByText() method searches the item collection for a ListItem with a Text property that equals the specified text. This method is case-sensitive and culture-insensitive. The null is returned if this method does not find an item.
The following ASP.NET C# example source code demonstrates to us how can we find a list item from DropDownList using specified item text.
ListItem object has a Text property that gets or sets the text displayed in DropDownList. And have a Value property that is associated with the ListItem. Value property value is hidden in the web browser.
We can find a DropDownList item by its Text property programmatically at run time. The .NET ListItemCollection class’s FindByText() method searches the item collection for a ListItem with a Text property that equals the specified text. This method is case-sensitive and culture-insensitive. The null is returned if this method does not find an item.
The following ASP.NET C# example source code demonstrates to 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>