Find an item by text from RadioButtonList
The following asp.net c# example code demonstrate us how can we find an item from RadioButtonList
programmatically at run time by item text. RadioButtonList is an asp.net list web server control. RadioButtonList
control generate a single selection/check radio group on web page.
RadioButtonList control contains each item represent a ListItem object. RadioButtonList all items exists in an items collection. So, we can manage (add, edit, delete) RadioButtonList items by using .net frameworks's Collection<T> Class methods and properties. Collection<T> Class FindByText() method allow us to find a ListItem object from a ListItem objects collection by its Text property value (text). Each ListItem object have a Text property and a Value property.
In this example code we call the Collection<T> Class FindByText() method by providing a ListItem object's Text property value and the method return the specified ListItem object which match the search criteria.
RadioButtonList control contains each item represent a ListItem object. RadioButtonList all items exists in an items collection. So, we can manage (add, edit, delete) RadioButtonList items by using .net frameworks's Collection<T> Class methods and properties. Collection<T> Class FindByText() method allow us to find a ListItem object from a ListItem objects collection by its Text property value (text). Each ListItem object have a Text property and a Value property.
In this example code we call the Collection<T> Class FindByText() method by providing a ListItem object's Text property value and the method return the specified ListItem object which match the search criteria.
RadioButtonListItemFindByText.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
string searchString = TextBox1.Text.ToString();
if (RadioButtonList1.Items.FindByText(searchString) != null)
{
Label1.Text = "Item Found and Select: " + searchString;
RadioButtonList1.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 RadioButtonList, FindByText()</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Red">RadioButtonList example: Find By Text</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Bold="true"
ForeColor="Green"
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:RadioButtonList
ID="RadioButtonList1"
runat="server"
BackColor="DodgerBlue"
ForeColor="Snow"
>
<asp:ListItem>CompareValidator</asp:ListItem>
<asp:ListItem>Localize</asp:ListItem>
<asp:ListItem>GridView</asp:ListItem>
<asp:ListItem>Repeater</asp:ListItem>
<asp:ListItem>SiteMapDataSource</asp:ListItem>
</asp:RadioButtonList>
<br /><br />
<asp:Label
ID="Label3"
runat="server"
ForeColor="DodgerBlue"
Text="Item Text"
>
</asp:Label>
<asp:TextBox
ID="TextBox1"
runat="server"
BackColor="DodgerBlue"
ForeColor="Snow"
>
</asp:TextBox>
<br />
<asp:Button
ID="Button1"
runat="server"
Text="Find Item"
Font-Bold="true"
ForeColor="DodgerBlue"
OnClick="Button1_Click"
/>
</div>
</form>
</body>
</html>


