Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在codebehind asp.net中默认选中的复选框列表项_C#_Asp.net_.net_Code Behind_Checkboxlist - Fatal编程技术网

C# 在codebehind asp.net中默认选中的复选框列表项

C# 在codebehind asp.net中默认选中的复选框列表项,c#,asp.net,.net,code-behind,checkboxlist,C#,Asp.net,.net,Code Behind,Checkboxlist,在我的页面中,我有一个复选框列表控件,上面有7项。我想将这7项设置为在我的Page\u loadcodebihind中选中的 我的页面: <asp:CheckBoxList ID="WeeklyCondition" runat="server"> <asp:ListItem Value="1">Sat</asp:ListItem> <asp:ListItem Value="2">Sun</asp:ListItem>

在我的页面中,我有一个
复选框列表
控件,上面有7项。我想将这7项设置为在我的
Page\u load
codebihind中选中的

我的页面:

<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>

坐
太阳
周一
星期二
结婚
清华大学
星期五
您可以使用循环遍历
复选框列表的items集合
,并更改
Selected
属性

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

如果您想检查某些具有某些条件的情况,可以使用以下方法:

protected void Page_Load(object sender, EventArgs e)
{
    for (int i = 0; i < CheckBoxList1.Items.Count; i++)
    {
        if(someCondition)
           CheckBoxList1.Items[i].Selected = true;
    }
}
受保护的无效页面加载(对象发送方,事件参数e)
{
对于(int i=0;i

如何将复选框列表项设置为默认选中

第一种方式:

<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>

项目1
项目2
项目3
项目4
项目5
第二种方式:

页面文件:

<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>

项目1
项目2
项目3
项目4
项目5
代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{
    for (int i = 0; i < CheckBoxList.Items.Count; i++)
    {
        CheckBoxList.Items[i].Selected = true;
    }
}
受保护的无效页面加载(对象发送方,事件参数e)
{
对于(int i=0;i

更好的是,在复选框列表的数据绑定事件中执行此操作。
<asp:ListItem Selected="True">Item1</asp:ListItem>