在codebehind asp.net中默认选中的checkboxlist项

Ami*_*ahi 8 .net c# asp.net code-behind checkboxlist

在我的页面中,我有一个CheckBoxList控件,我有7个项目.我想在我的Page_load代码生成中设置这7个项目.

我的页面:

<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>
Run Code Online (Sandbox Code Playgroud)

Adi*_*dil 10

您可以使用循环遍历项集合CheckBoxList并更改Selected属性.

foreach (ListItem item in WeeklyCondition.Items) 
    item.Selected = true;
Run Code Online (Sandbox Code Playgroud)


Maj*_*jid 6

如果你想检查一些有条件的东西,你可以使用这样的东西:

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

这里开始