Reputation: 12711
I have a Listview control "lstStudents" and i have added checkboxes inside the List viewControl.I need to add a Select All check box which results in checking all the checkboxes inside the ListView i use the following code but it doesn't work.
private void chkAll_CheckedChanged(object sender, EventArgs e)
{
foreach (Control cont in lstStudents.Controls)
{
if (cont.GetType() == typeof(CheckBox))
{
(cont as CheckBox).Checked = true;
}
}
}
I'm using c# windows Forms......
Upvotes: 1
Views: 272
Reputation: 21864
You are talking to the dataitem instead of the control itself
private void chkAll_CheckedChanged(object sender, EventArgs e)
{
foreach (ListViewItem item in lstStudents.Items)
{
item.Checked = chkAll.Checked;
}
}
so there is no need for an extra reference validation on these items
Upvotes: 2
Reputation: 7430
Try this:
private void chkAll_CheckedChanged(object sender, EventArgs e)
{
foreach (ListViewDataItem item in lstStudents.Items)
{
CheckBox cbSelect = item.FindControl("cbSelect") as CheckBox;
if (cbSelect != null)
{
cbSelect.Checked = true;
}
}
}
Assuming your listview definition goes something like this:
<asp:listview runat="server">
<itemtemplate>
<asp:checkbox id="cbSelect" runat="server" />
</itemtemplate>
</asp:listview>
Upvotes: 1