禁用或检查以前选中的checkboxlist项目?

时间:2013-08-29 17:50:58

标签: asp.net

我想禁用或检查以前选择的项目。所选项目保存在数据库中,因为userid_checkbox iteams.i需要由用户获取所选项目。如果用户id为10且所选项为2,则它将保存在数据库10_2中。所以我需要拆分它以获取第二个项目编号并使用循环来禁用所选项目。我写的如下

        int user_id = Convert.ToInt16(Session["user_id"]);
        ward w = new ward();
        using (DataClassesDataContext db = new DataClassesDataContext())
        {
            List<string> bednum = (from j in db.wards where w.user_id == user_id select j.wbedno).ToList();
             foreach (var bed in bednum)
             {
                 string vals = bed.Split('_')[1];
                 cbList = (CheckBoxList)pnlControls.FindControl(vals);
                 //  cbList.Items.Add(new ListItem(vals));


                 var query = from listItem in cbList.Items.Cast<ListItem>()
                             //   join item in bednum  on
                             where listItem.Value == bed
                             select listItem;

                 //   var query =  from l  in cbList.Items
                 foreach (ListItem listItem in query)
                     listItem.Selected = true;

             }
        }

1 个答案:

答案 0 :(得分:0)

将您的代码更改为:

int user_id = Convert.ToInt16(Session["user_id"]);
CheckBoxList cbList = (CheckBoxList)pnlControls.FindControl("MyCheckBoxListID");
ward w = new ward();//Guessing ward is a class?

using (DataClassesDataContext db = new DataClassesDataContext())
{
    List<string> bednum = (from j in db.wards where w.user_id == user_id select j.wbedno).ToList();
    foreach (var bed in bednum)
    {
        string vals = bed.Split('_')[1];
        ListItem listItem = cbList.Items.FindByValue(vals);
        if (listItem != null) listItem.Selected = true;
    }
}