foreach(ListItem l in CheckBoxList1.Items)
{
    l.Selected = true;
}我断点后发现,上面这个循环不执行 ListItem l 这句,到 in 的时候就结束了。请问怎么才能正确循环 CheckBoxList1 内的每一个 Item 呢?

解决方案 »

  1.   

    你的方法是对的,也许你的 CheckBoxList1.Items.Count == 0
      

  2.   

    foreach语句中的ListItem l 是只读的,不能改变他的值
    用for吧
      

  3.   

    string str = "";
                foreach (ListItem li in cblrighgs.Items)
                {
                    if (li.Selected == true)
                    {
                        str += li.Value+";";
                    }
                }            Response.Write(str);
      

  4.   

    循环的代码没有错,但估计是地方错了确定循环的时候CheckBoxList1已完成绑定或已根据ViewState初始化
      

  5.   

    这样写没问题啊,是因为你的 CheckBoxList1.Items本来就没有Item吧?