checkboxlist items as checked by default in codebehind asp.net

a 夏天 提交于 2019-12-05 03:19:35
Majid

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

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;
Rohit Mane

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;
    }
}
<asp:ListItem Selected="True">Item1</asp:ListItem>

How can I set checkboxlist items as checked by default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!