想判断checkBoxlist项内容和数组中的内容是否相同?如果相同checkBoxList项打勾,具体代码怎么写? 
这样写不行 
for (int j = 0; j  < CheckBoxList1.Items.Count; j++) 
            { 
                string[] supplierName = supplier.Split(',');   //supplier是从数据库取出的值 
                foreach (string i in supplierName) 
                { 
                    if (CheckBoxList1.Items[j].Text == i) 
                    { 
                        CheckBoxList1.Items[j].Selected = true; 
                    } 
                } 
            } 

解决方案 »

  1.   

    没问题啊,我可以执行阿。我拷贝你的代码,放了个CheckBoxList1,里面加了3个checkbox
    Text分别是1,2,3.
    string supplier = "1,2,5";
    for (int j = 0; j < CheckBoxList1.Items.Count; j++)
    {
         string[] supplierName = supplier.Split(',');   //supplier是从数据库取出的值  
          foreach (string i in supplierName)
         {
             if (CheckBoxList1.Items[j].Text == i)
             {
                 CheckBoxList1.Items[j].Selected = true;
             }
         }
    }  
    执行后1和2被打上钩了。没错呀~~
      

  2.   

    supplier变量你在debug下阿,看看有没有取道
      

  3.   

    完整代码:
    protected void Page_Load(object sender, EventArgs e)
        {
            string supplier = string.Empty;        if (this.IsPostBack == false)
            {         
                string id = Request.QueryString["id"];
                SqlDataReader reader;
                reader = news.SelectCustomerId(id);
                while (reader.Read())
                {
                    TextBox3.Text = reader["CIDCard"].ToString();
                    TextBox4.Text = reader["CCompany"].ToString();
                    TextBox6.Text = reader["CReceiver"].ToString();                TextBox1.Text = reader["CBillInfo"].ToString();
                    TextBox2.Text = reader["CShipInfo"].ToString();                supplier = reader["CHasSuppliers"].ToString();
                }
                for (int j = 0; j < CheckBoxList1.Items.Count; j++)
                {
                    string[] supplierName = ss.Split(',');   
                    foreach (string i in supplierName)
                    {
                        if (CheckBoxList1.Items[j].Text == i)
                        {
                            CheckBoxList1.Items[j].Selected = true;
                        }
                    }
                }  
            }
        }
    不报错,但读不出数据.checklist是绑定数据库的!!!!!!!!!!!!!!!!!!!!!!!
      

  4.   

    上面写错!!!!!!!!!!!!!!!!!!!!!!!!!!是这个!!!!!!!!!!!!!!!!!!!!!!!!
    完整代码: 
    protected void Page_Load(object sender, EventArgs e) 
        { 
            string supplier = string.Empty;         if (this.IsPostBack == false) 
            {          
                string id = Request.QueryString["id"]; 
                SqlDataReader reader; 
                reader = news.SelectCustomerId(id); 
                while (reader.Read()) 
                { 
                    TextBox3.Text = reader["CIDCard"].ToString(); 
                    TextBox4.Text = reader["CCompany"].ToString(); 
                    TextBox6.Text = reader["CReceiver"].ToString();                 TextBox1.Text = reader["CBillInfo"].ToString(); 
                    TextBox2.Text = reader["CShipInfo"].ToString();                 supplier = reader["CHasSuppliers"].ToString(); 
                } 
                for (int j = 0; j  < CheckBoxList1.Items.Count; j++) 
                { 
                    string[] supplierName = supplier.Split(',');    
                    foreach (string i in supplierName) 
                    { 
                        if (CheckBoxList1.Items[j].Text == i) 
                        { 
                            CheckBoxList1.Items[j].Selected = true; 
                        } 
                    } 
                }   
            } 
        } 
    不报错,但读不出数据.checklist是绑定数据库的!!!!!!!!!!!!!!!!!!!!!!!
      

  5.   

    CheckBoxList1.DataBind(); 在后面加个```