我有5个带有CheckBoxList1、CheckBoxList2等ID的CheckBoxList控件,它们都有相同的列表项。
现在,当我编写下面这行代码时:
CheckBoxList1.Items[0].Selected = true;它选择了CheckBoxList1的第一项,但所有其他CheckBoxList的第一项也被选中了。你知道为什么会发生这样神秘的事情吗?
所有的CheckBoxList具有相同数量的项目,每个项目具有相同的文本和相同的值。
它们是用从数据库获取的数据动态填充的。
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM EMPLOYEE_TABLE WHERE EMPLOYEE_TABLE.EmployeeID NOT IN (SELECT ORG_UNIT.ManagerID FROM ORG_UNIT WHERE ORG_UNIT.OrgUnitID = '" + teamid + "') AND EMPLOYEE_TABLE.OrgUnitID = '" + teamid + "'",con);
DataSet da = new DataSet();
DataTable table = new DataTable();
adapter.Fill(table);
adapter.Fill(da);
int count = da.Tables[0].Rows.Count;
CheckBoxList1.Items.Clear();
CheckBoxList2.Items.Clear();
CheckBoxList3.Items.Clear();
CheckBoxList4.Items.Clear();
CheckBoxList5.Items.Clear();
no_of_listitem = count;
for (int i = 0; i < table.Rows.Count; i++)
{
ListItem temp = new ListItem();
temp.Text = table.Rows[i]["FName"].ToString();
employeeid[i] = table.Rows[i]["EmployeeID"].ToString();
temp.Value = i.ToString();
CheckBoxList1.Items.Add(temp);
CheckBoxList2.Items.Add(temp);
CheckBoxList3.Items.Add(temp);
CheckBoxList4.Items.Add(temp);
CheckBoxList5.Items.Add(temp);
} 发布于 2011-04-20 18:42:17
这可能是因为您填充这些列表的方式...
我猜您正在将相同的对象添加到每个列表中。因此,对任何对象的任何修改都会影响所有列表。
使用以下语句:
CheckBoxList.Items.Add(New ListItem(table.Rows[i]["FName"].ToString(),
table.Rows[i]["EmployeeID"].ToString())); 发布于 2011-04-20 18:58:28
Akram是正确的-所有的CheckBoxList不仅包含看起来相同的条目,而且每个条目都是相同的。所以,为了直接回答你的问题,你需要为每个CheckBoxList添加一个new ListItem,例如:
for (int i = 0; i < table.Rows.Count; i++)
{
var firstName = table.Rows[i]["FName"].ToString()
var employeeId = table.Rows[i]["EmployeeID"].ToString();
CheckBoxList1.Items.Add(new ListItem { Text = firstName, Value = employeeId });
CheckBoxList2.Items.Add(new ListItem { Text = firstName, Value = employeeId });
CheckBoxList3.Items.Add(new ListItem { Text = firstName, Value = employeeId });
CheckBoxList4.Items.Add(new ListItem { Text = firstName, Value = employeeId });
CheckBoxList5.Items.Add(new ListItem { Text = firstName, Value = employeeId });
}发布于 2011-04-20 19:02:08
for (int i = 0; i < table.Rows.Count; i++)
{
ListItem temp = new ListItem();
temp.Text = table.Rows[i]["FName"].ToString();
employeeid[i] = table.Rows[i]["EmployeeID"].ToString();
temp.Value = i.ToString();
CheckBoxList1.Items.Add(temp);
ListItem temp = new ListItem();
temp.Text = table.Rows[i]["FName"].ToString();
employeeid[i] = table.Rows[i]["EmployeeID"].ToString();
temp.Value = i.ToString();
CheckBoxList2.Items.Add(temp);
ListItem temp = new ListItem();
temp.Text = table.Rows[i]["FName"].ToString();
employeeid[i] = table.Rows[i]["EmployeeID"].ToString();
temp.Value = i.ToString();
CheckBoxList3.Items.Add(temp);
ListItem temp = new ListItem();
temp.Text = table.Rows[i]["FName"].ToString();
employeeid[i] = table.Rows[i]["EmployeeID"].ToString();
temp.Value = i.ToString();
CheckBoxList4.Items.Add(temp);
ListItem temp = new ListItem();
temp.Text = table.Rows[i]["FName"].ToString();
employeeid[i] = table.Rows[i]["EmployeeID"].ToString();
temp.Value = i.ToString();
CheckBoxList5.Items.Add(temp);
} https://stackoverflow.com/questions/5728943
复制相似问题