复选框列表问题

发布于 2024-11-03 01:23:18 字数 1408 浏览 0 评论 0原文

我有 5 个 CheckBoxList 控件,其 IDCheckBoxList1CheckBoxList2 等。它们内有相同的列表项。

现在,当我编写以下行时:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南城追梦 2024-11-10 01:23:18

这可能是因为您填充这些列表的方式......

我猜您正在向每个列表添加相同的对象。因此,对任何对象的任何修改都会影响所有列表。


使用以下语句:

    CheckBoxList.Items.Add(New ListItem(table.Rows[i]["FName"].ToString(), 
table.Rows[i]["EmployeeID"].ToString())); 

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:

    CheckBoxList.Items.Add(New ListItem(table.Rows[i]["FName"].ToString(), 
table.Rows[i]["EmployeeID"].ToString())); 
等你爱我 2024-11-10 01:23:18

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 });
}

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 a new ListItem for each CheckBoxList, for instance:

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 });
}
红ご颜醉 2024-11-10 01:23:18
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);
} 
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);
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文