0

我想禁用或检查以前选择的项目。选定的项目作为 userid_checkbox iteams 保存在数据库中。我需要由用户获取选定的项目。如果用户 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;

             }
        }
4

1 回答 1

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;
    }
}
于 2013-08-30T00:17:04.577 回答