检查后面的CheckBoxList项检查代码

时间:2012-02-02 11:55:41

标签: c# linq

我有两张桌子:

  1. has_service
  2. 服务。
  3. 我想获取表1中的项目,然后将其与第二个表进行比较,看看是否匹配。该项目应显示为已选中。

    我的代码有问题吗?

    ListItem thItm;
    
    //List all services in system DB.
    var AllSrvs = from s in db.services                      
                  select s;
    
    //Get current request-services ID's
    var reqSrvs = (from rs in db.has_services
                   where rs.request_id == Convert.ToInt32(Request["reqID"].Trim())
                   select rs.service_id).ToArray();
    
    foreach (service se in AllSrvs)
    {
        thItm = new ListItem();
        thItm.Text = se.service_name_en;
        thItm.Value = se.service_id.ToString();
    
    
    
        for (int i = 0; i < reqSrvs.Count();i++ )
        {
    
            if (reqSrvs[i] ==Convert.ToInt32( se.service_id))
            {
                thItm.Selected = true;
    
            }
            else
            {
                thItm.Selected = false;
            }               
        }
    
        RequestServicesCheckBoxList.Items.Add(thItm);
    }
    

1 个答案:

答案 0 :(得分:1)

谢谢大家。 :) 我想通了:) 问题与“其他”部分有关。  删除它,它现在正常工作......

 ListItem thItm;

//List all services in system DB.
var AllSrvs = from s in db.services                      
          select s;

//Get current request-services ID's
var reqSrvs = (from rs in db.has_services
           where rs.request_id == Convert.ToInt32(Request["reqID"].Trim())
           select rs.service_id).ToArray();

 foreach (service se in AllSrvs)
  {
thItm = new ListItem();
thItm.Text = se.service_name_en;
thItm.Value = se.service_id.ToString();



for (int i = 0; i < reqSrvs.Count();i++ )
{

    if (reqSrvs[i] ==Convert.ToInt32( se.service_id))
    {
        thItm.Selected = true;

    }
   // else
   // {
   //     thItm.Selected = false;
   // }               
}

RequestServicesCheckBoxList.Items.Add(thItm);

}