想判断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;
}
}
}
这样写不行
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;
}
}
}
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被打上钩了。没错呀~~
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是绑定数据库的!!!!!!!!!!!!!!!!!!!!!!!
完整代码:
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是绑定数据库的!!!!!!!!!!!!!!!!!!!!!!!