复选框列表问题
我有 5 个 CheckBoxList
控件,其 ID
为 CheckBoxList1
、CheckBoxList2
等。它们内有相同的列表项。
现在,当我编写以下行时:
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);
}
I have 5 CheckBoxList
controls with ID
's of CheckBoxList1
,CheckBoxList2
, and so on. They have same list items within them.
Now when I write the following line:
CheckBoxList1.Items[0].Selected = true;
It selects the 1st item of CheckBoxList1
but the 1st item of all the other CheckBoxList
's gets selected as well. Any idea why is such a mysterious thing happening?
All the CheckBoxList
's have the same number of items, with each item having the same text and the same value.
They are populated dynamically with data being fetched from the database.
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能是因为您填充这些列表的方式......
我猜您正在向每个列表添加相同的对象。因此,对任何对象的任何修改都会影响所有列表。
使用以下语句:
That's probably because of the way you are populating those lists ...
I guess you are adding the same objects to each list. So any modification on any object effects all lists ..
Use the following statement:
Akram 是正确的 - 所有
CheckBoxList
不仅包含看起来相同的项目,而且它们每一项都相同。因此,为了直接回答您的问题,您需要为每个CheckBoxList
添加一个new ListItem
,例如:Akram is correct - all the of
CheckBoxList
's contain not only items that look the same, they are each one in the same. So, in order to answer your question directly, you'll need to add anew ListItem
for eachCheckBoxList
, for instance: