I have a checkboxlist which gets its items from a sql table and change based on what is selected from a drop down menu. I'm trying to add another checkbox in that list which acts as a select/deselect all box. I've seen a few examples around this site, but nothing seems to work quite right.
Intended Functionality: When box is selected, all the other boxes are selected.
When box is un-selected, all the other boxes are selected.
When box is selected, if another box is then unselected, the select all box is unselected.
C#:
List<ListItem> toBeRemoved = new List<ListItem>();
//removes items when new item from dropdown is selected
for (int i = 1; i < CheckOpenTimesheets.Items.Count; i++)
{
toBeRemoved.Add(CheckOpenTimesheets.Items[i]);
}
for (int i = 0; i < toBeRemoved.Count; i++)
{
CheckOpenTimesheets.Items.Remove(toBeRemoved[i]);
}
lblOpenTimesheets.Text = "";
String sql = "sql statement here";
command.CommandText = sql;
command.Parameters.Add(new SqlParameter("userid", ddlActingAs.SelectedValue.ToString()));
SqlDataReader reader = command.ExecuteReader();
//Adds items to the checkboxlist
while (reader.Read())
{
ListItem item = new ListItem();
item.Text += reader.GetDateTime(0).ToString("MM/dd/yyyy") + " is open";
item.Value = reader["StartDate"].ToString();
CheckOpenTimesheets.Items.Add(item);
}
CheckOpenTimesheets.UpdateAfterCallBack = true;
reader.Close();
What I tried for the selection/deselection:
protected void select_DeselectAll(object sender, System.EventArgs e)
{
if (CheckOpenTimesheets.Items[0].Selected == true)
{
foreach (ListItem item in CheckOpenTimesheets.Items)
{
item.Selected = true;
}
}
else
{
foreach (ListItem item in CheckOpenTimesheets.Items)
{
item.Selected = false;
}
}
}
ASP:
<anthem:CheckBoxList ID="CheckOpenTimesheets" OnSelectedIndexChanged="select_DeselectAll" runat="server" AutoPostBack="true" >
<asp:ListItem Text="Select/Deselect All" />
</anthem:CheckBoxList>
Edit: The problem seems to be the OnSelectedIndexChanged event firing when the checkboxes other than the select all one is selected.
Edit2: Made the select all checkbox separate from the checkboxlist. However, when trying to implement the feature where if a checkbox is unselected when the select all checkbox is checked, the select all checkbox is then deselected BUT all the others don't "deselect". This isn't happening though because the select all checkbox event fires.
protected void chkAll_CheckedChanged(object sender, EventArgs e)
{
foreach (ListItem item in CheckOpenTimesheets.Items)
{
item.Selected = chkAll.Checked;
}
}
protected void checkbox_Selected(object sender, EventArgs e)
{
chkAll.CheckedChanged -= chkAll_CheckedChanged;
foreach (ListItem item in CheckOpenTimesheets.Items)
{
if ((item.Selected = false) && (chkAll.Checked = true))
{
chkAll.Checked = false;
}
}
}
It sounds like you understand your issue. The problem comes when you click any other checkbox other than the first one. If the first is not checked and you check another checkbox, your logic is telling all of the other checkboxes to not be checked, including the one you just checked.
One option would be to take the "Select/Deselect All" checkbox out of the CheckBoxList. Let it be its own standalone checkbox.
<asp:CheckBox ID="chkAll" runat="server" Text="Select/Deselect All" OnCheckedChanged="chkAll_CheckedChanged" AutoPostBack="true" />
<anthem:CheckBoxList ID="checkOpenTimesheets" OnSelectedIndexChanged="checkOpenTimesheets_SelectedIndexChanged" runat="server" AutoPostBack="true" >
</anthem:CheckBoxList>
Then your select all event handler would just change them all to be the same.
protected void chkAll_CheckedChanged(object sender, EventArgs e)
{
foreach(ListItem item in CheckOpenTimesheets.Items)
{
item.Selected = chkAll.Checked;
}
}
Then also build in functionality where you programatically select or deselect the "Select/Deselect All" checkbox if any in the CheckBoxList change to something other than what all the others are. As explained in this answer, the key here would be to turn off the CheckedChanged event of the select all checkbox, otherwise that event will fire as you have seen.
protected void checkOpenTimesheets_SelectedIndexChanged(object sender, EventArgs e)
{
chkAll.CheckedChanged -= chkAll_CheckedChanged;
CheckBoxList checkOpenTimesheets = (CheckBoxList)sender;
if (allItemsCheckedInCheckBoxList(checkOpenTimesheets))
{
chkAll.Checked = true;
}
else if (allItemsUnCheckedInCheckBoxList(checkOpenTimesheets))
{
chkAll.Checked = false;
}
chkAll.CheckedChanged += chkAll_CheckedChanged;
}
private bool allItemsCheckedInCheckBoxList(CheckBoxList checkBoxList)
{
bool allItemsChecked = true;
foreach (ListItem item in checkBoxList.Items)
{
allItemsChecked = item.Selected;
if (!allItemsChecked)
break;
}
return allItemsChecked;
}
private bool allItemsUnCheckedInCheckBoxList(CheckBoxList checkBoxList)
{
bool allItemsUnChecked = false;
foreach (ListItem item in checkBoxList.Items)
{
allItemsUnChecked = item.Selected;
if (allItemsUnChecked)
break;
}
return allItemsUnChecked;
}