Find list item by value in ListBox
asp.net listbox web server control allow users to select multiple items. by default
listbox allow select one item from the control. listbox Items property get the collection of items in the listbox.
so Items property gets the items collection of listbox control.
listbox list contains ListItem objects. ListItem object have Text and Value properties. Text property text display in listbox and Value property text is hidden in web browser and associated with item.
asp.net developers can find item by its value from a listbox control. listbox Items.FindByValue(String) method allow us to find an item with specified Value from its items collection. FindByValue(String) method requires a string parameter that contain specified Value string.
the following c# example source code demonstrate us how can we find (search) listbox item by its value in asp.net.
listbox list contains ListItem objects. ListItem object have Text and Value properties. Text property text display in listbox and Value property text is hidden in web browser and associated with item.
asp.net developers can find item by its value from a listbox control. listbox Items.FindByValue(String) method allow us to find an item with specified Value from its items collection. FindByValue(String) method requires a string parameter that contain specified Value string.
the following c# example source code demonstrate us how can we find (search) listbox item by its value in asp.net.
ListBoxItemFindByValue.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
string searchText = TextBox1.Text.ToString();
if (ListBox1.Items.FindByValue(searchText) != null)
{
Label1.Text = "Item Found, Value: " + searchText;
ListBox1.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 ListBox, FindByValue()</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Red">ListBox 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="asp.net controls"
Font-Bold="true"
ForeColor="DarkGreen"
>
</asp:Label>
<br />
<asp:ListBox
ID="ListBox1"
runat="server"
BackColor="DarkGreen"
ForeColor="FloralWhite"
>
<asp:ListItem Value="1">ImageMap</asp:ListItem>
<asp:ListItem Value="2">Literal</asp:ListItem>
<asp:ListItem Value="3">ListView</asp:ListItem>
<asp:ListItem Value="4">RegularExpressionValidator</asp:ListItem>
<asp:ListItem Value="5">ValidationSummary</asp:ListItem>
<asp:ListItem Value="6">Menu</asp:ListItem>
</asp:ListBox>
<br /><br />
<asp:Label
ID="Label3"
runat="server"
ForeColor="SeaGreen"
Text="Item Text"
>
</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>


