Link to home
Start Free TrialLog in
Avatar of AlHal2
AlHal2Flag for United Kingdom of Great Britain and Northern Ireland

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?

 <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> 

Open in new window

Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true) {
                checkBox2.Checked = true;
                checkBox3.Checked = true;
                }
it is in a GridView so you will have to iterate all the rows...

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

Open in new window

Avatar of AlHal2

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?)
Avatar of AlHal2

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"/>
ASKER CERTIFIED SOLUTION
Avatar of Robberbaron (robr)
Robberbaron (robr)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AlHal2

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.