checkboxlist items as checked by default in codebehind asp.net
Asked Answered
D

4

10

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>
Disvalue answered 27/7, 2014 at 6:11 Comment(0)
C
8

If you want to check some of those with some condition, You can use something like this :

protected void Page_Load(object sender, EventArgs e)
{
    for (int i = 0; i < CheckBoxList1.Items.Count; i++)
    {
        if(someCondition)
           CheckBoxList1.Items[i].Selected = true;
    }
}

from here

Canonicals answered 27/7, 2014 at 6:33 Comment(1)
Better yet, do it in the checkboxlist's DataBound event.Kurtz
F
12

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;
Fancied answered 27/7, 2014 at 6:14 Comment(0)
C
8

If you want to check some of those with some condition, You can use something like this :

protected void Page_Load(object sender, EventArgs e)
{
    for (int i = 0; i < CheckBoxList1.Items.Count; i++)
    {
        if(someCondition)
           CheckBoxList1.Items[i].Selected = true;
    }
}

from here

Canonicals answered 27/7, 2014 at 6:33 Comment(1)
Better yet, do it in the checkboxlist's DataBound event.Kurtz
B
3

How can I set checkboxlist items as checked by default

First way:

<asp:CheckBoxList runat="server" ID="CheckBoxList1">
    <asp:ListItem Selected="True">Item1</asp:ListItem>
    <asp:ListItem Selected="True">Item2</asp:ListItem>
    <asp:ListItem Selected="True">Item3</asp:ListItem>
    <asp:ListItem Selected="True">Item4</asp:ListItem>
    <asp:ListItem Selected="True">Item5</asp:ListItem>
</asp:CheckBoxList>

Second way:

Page File:

<asp:CheckBoxList runat="server" ID="CheckBoxList">
    <asp:ListItem>Item1</asp:ListItem>
    <asp:ListItem>Item2</asp:ListItem>
    <asp:ListItem>Item3</asp:ListItem>
    <asp:ListItem>Item4</asp:ListItem>
    <asp:ListItem>Item5</asp:ListItem>
</asp:CheckBoxList>

CodeBehind:

protected void Page_Load(object sender, EventArgs e)
{
    for (int i = 0; i < CheckBoxList.Items.Count; i++)
    {
        CheckBoxList.Items[i].Selected = true;
    }
}
Bootlace answered 16/3, 2016 at 13:20 Comment(0)
M
0
<asp:ListItem Selected="True">Item1</asp:ListItem>

How can I set checkboxlist items as checked by default

Moser answered 14/2, 2017 at 6:17 Comment(1)
OK I am loading a simple table with values that are all checked by default. The loop to read is below. The critical part I think you want is: cblAnalyteGroup.Items.Add(dRow.Item("AnalyteGroupName").Selected) The code for the page is very limited: <asp:CheckBoxList SelectionMode="Multiple" ID="cblAnalyteGroup" runat="server" /> For Each dRow As DataRow In cblAnalyteGroup.DataSource.Rows cblAnalyteGroup.Items.Add(dRow.Item("AnalyteGroupName").Selected) NextThyestes

© 2022 - 2024 — McMap. All rights reserved.