asp.net checkboxlist set checked items
In this tutorial we will checked specified items from CheckBoxList programmatically.
To checked specified items, we searched all items from CheckBoxList which starts with "A". We srearched and get a list of items by using a Linq query. StartWith() function allow us to check whether the provided string starts with specific character or characters.
When we get the list of items to be checked programmatically then we loop through the items. Next, we set the specified items Selected property value to True. We do this in Page_Load event and we also check the page is not PostBack state. So first time when the CheckBoxList is loaded, we checked the specified items.
The following asp.net c# example code demonstrate us how can we set the few items checked programmatically based on condition within a CheckBoxList.
To checked specified items, we searched all items from CheckBoxList which starts with "A". We srearched and get a list of items by using a Linq query. StartWith() function allow us to check whether the provided string starts with specific character or characters.
When we get the list of items to be checked programmatically then we loop through the items. Next, we set the specified items Selected property value to True. We do this in Page_Load event and we also check the page is not PostBack state. So first time when the CheckBoxList is loaded, we checked the specified items.
The following asp.net c# example code demonstrate us how can we set the few items checked programmatically based on condition within a CheckBoxList.
checkboxlist-set-checked-items.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
string[] birds = {
"American Golden Plover",
"Collared Pratincole",
"American Woodcock",
"Herring Gull",
"Australian Pratincole"
};
CheckBoxList1.DataSource = birds;
CheckBoxList1.DataBind();
var checkedItems = from ListItem li in CheckBoxList1.Items
where li.Text.StartsWith("A")
select li;
foreach (ListItem li in checkedItems)
{
li.Selected = true;
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net checkboxlist set checked items</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
asp.net example - checkboxlist set checked items
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Text="check item(s) from checkboxlist."
Font-Size="Medium"
Width="350"
>
</asp:Label>
<br /><br />
<div class="block">
<asp:CheckBoxList
ID="CheckBoxList1"
runat="server"
RepeatLayout="Table"
RepeatColumns="2"
AutoPostBack="true"
>
</asp:CheckBoxList>
</div>
</div>
</form>
</body>
</html>

- How to sort DropDownList items in descending order
- How to sort ListBox items
- How to set ListBox alternate item text and background color
- How to select all items in a CheckBoxList
- How to retrieve multiple selected values from CheckBoxList
- How to create a horizontal CheckBoxList
- How to show ScrollBar in a CheckBoxList
- How to get CheckBoxList checked items
- How to get CheckBoxList selected values using Linq
- How to set the maximum limit of selection in a CheckBoxList