AlHal2
asked on
Allow users to select all checkboxes
I have a gridview in a c# webform.
When the user selects the top left checkbox (chkAll), I want it to select all the checkboxes below. How do I do this?
When the user selects the top left checkbox (chkAll), I want it to select all the checkboxes below. How do I do this?
<div style="width:1000px;height:400px;overflow:auto; ">
<asp:GridView ID="GridView1" runat="server"
AllowSorting="True" Font-Names="Arial" Font-Size="Small"
RowStyle-Wrap="False" BackColor="White" BorderColor="Silver"
AutoGenerateColumns="True"
onsorting="GridView1_Sorting"
onselectedindexchanged="GridView1_SelectedIndexChanged"
onrowdatabound="GridView1_RowDataBound">
<RowStyle Wrap="False" />
<Columns>
<asp:CommandField ShowSelectButton="False" />
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox runat="server" ID="chkAll" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkSelected" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RIC" >
<ItemTemplate>
<asp:LinkButton ID="lnk_productid" Text='<%#Eval("RIC") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
it is in a GridView so you will have to iterate all the rows...
in code behind....
in code behind....
protected void chkAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox cbAll = (CheckBox) sender;
ToggleCheckState(cbAll.Checked);
}
private void ToggleCheckState(bool checkState)
{
// Iterate through the Products.Rows property
foreach (GridViewRow rx in GridView1.Rows)
{
CheckBox cblst = (CheckBox)rx.FindControl("chkSelected");
if (cblst != null)
{
cblst.Selected = checkState;
}
}
}
}
ASKER
On the line cblst.Selected = checkState' I'm getting a message
'System.Web.UI.WebControls .CheckBox' does not contain a definition for 'Selected' and no extension method 'Selected' accepting a first argument of type 'System.Web.UI.WebControls .CheckBox' could be found (are you missing a using directive or an assembly reference?)
'System.Web.UI.WebControls
ASKER
I changed cblst.Selected to Cblst.Changed and that removed the error.
However when I actually select the chkall box the code behind does not get called even though I added this into the gridview source
<asp:CheckBox runat="server" ID="chkAll" OnCheckedChanged ="chkAll_CheckedChanged"/>
However when I actually select the chkall box the code behind does not get called even though I added this into the gridview source
<asp:CheckBox runat="server" ID="chkAll" OnCheckedChanged ="chkAll_CheckedChanged"/>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I did set a break point on the event handler and it wasn't called.
The only way I got it to work was to set autopostback=true.
It can be a bit slow if there are lots of checkboxes, but I think it's OK.
I'll sign this off. If you have any other advice, please let me know.
The only way I got it to work was to set autopostback=true.
It can be a bit slow if there are lots of checkboxes, but I think it's OK.
I'll sign this off. If you have any other advice, please let me know.
{
if (checkBox1.Checked == true) {
checkBox2.Checked = true;
checkBox3.Checked = true;
}