checkboxlist items as checked by default in codebehind asp.net

Amir Abdollahi picture Amir Abdollahi · Jul 27, 2014 · Viewed 26.9k times · Source

In my page I have a CheckBoxList control and I have 7 items on it. I would like to set those 7 items as checked in my Page_load codebihind.

my page:

<asp:CheckBoxList ID="WeeklyCondition" runat="server">
    <asp:ListItem Value="1">Sat</asp:ListItem>
    <asp:ListItem Value="2">Sun</asp:ListItem>
    <asp:ListItem Value="3">Mon</asp:ListItem>
    <asp:ListItem Value="4">Tue</asp:ListItem>
    <asp:ListItem Value="5">Wed</asp:ListItem>
    <asp:ListItem Value="6">Thu</asp:ListItem>
    <asp:ListItem Value="7">Fri</asp:ListItem>

</asp:CheckBoxList>

Answer

Adil picture Adil · Jul 27, 2014

You can use loop to iterate through the items collection of CheckBoxList and change the Selected property.

foreach (ListItem item in WeeklyCondition.Items) 
    item.Selected = true;