scooby
scooby

Reputation: 603

checkboxlist issue

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

Upvotes: 2

Views: 3315

Answers (3)

Gabriel
Gabriel

Reputation: 1443

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

Upvotes: 1

Grant Thomas
Grant Thomas

Reputation: 45068

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

Upvotes: 1

Akram Shahda
Akram Shahda

Reputation: 14781

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

Upvotes: 2

Related Questions