Find an item by item text from CheckBoxList
The following asp.net c# example code demonstrate us how can we find/search an item from CheckBoxList
by item's text. CheckBoxList is an asp.net list web server control. CheckBoxList control contains items
as ListItem object. Each ListItem object have a Text property and optionally a Value property. We can find/search CheckBoxList
item based on both ListItem object's 'Text' and 'Value' property value.
CheckBoxList control's items exists in an items collection object. So we can use .net framework's Collection<T> Class to manage CheckBoxList items. Collection<T> Class FindByText() method allow us to find an item from CheckBoxList items collection by the specified item's Text. If the method found any matches then it return the specified ListItem object; otherwise method return null.
CheckBoxList control's items exists in an items collection object. So we can use .net framework's Collection<T> Class to manage CheckBoxList items. Collection<T> Class FindByText() method allow us to find an item from CheckBoxList items collection by the specified item's Text. If the method found any matches then it return the specified ListItem object; otherwise method return null.
CheckBoxListFindItemByText.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
string searchString = TextBox1.Text.ToString();
if (CheckBoxList1.Items.FindByText(searchString) != null)
{
Label1.Text = "Item Found: " + searchString;
CheckBoxList1.Items.FindByText(searchString).Selected = true;
}
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 CheckBoxList, FindByText()</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Green">CheckBoxList example: Find By Text</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Bold="true"
ForeColor="HotPink"
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:CheckBoxList
ID="CheckBoxList1"
runat="server"
BackColor="DarkGreen"
ForeColor="FloralWhite"
>
<asp:ListItem>XML</asp:ListItem>
<asp:ListItem>PlaceHolder</asp:ListItem>
<asp:ListItem>LayoutEditorPart</asp:ListItem>
<asp:ListItem>PropertyGridEditorPart</asp:ListItem>
<asp:ListItem>DetailsView</asp:ListItem>
</asp:CheckBoxList>
<br /><br />
<asp:Label
ID="Label3"
runat="server"
ForeColor="DarkGreen"
Text="Item Text"
>
</asp:Label>
<asp:TextBox
ID="TextBox1"
runat="server"
BackColor="DarkGreen"
ForeColor="Snow"
>
</asp:TextBox>
<br />
<asp:Button
ID="Button1"
runat="server"
Text="Find Item"
Font-Bold="true"
ForeColor="DarkGreen"
OnClick="Button1_Click"
/>
</div>
</form>
</body>
</html>


