Solved

Allow users to select all checkboxes

Posted on 2016-10-11
6
59 Views
Last Modified: 2016-10-13
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

0
Comment
Question by:AlHal2
  • 3
  • 2
6 Comments
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true) {
                checkBox2.Checked = true;
                checkBox3.Checked = true;
                }
0
 
LVL 32

Expert Comment

by:Robberbaron (robr)
Comment Utility
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

0
 

Author Comment

by:AlHal2
Comment Utility
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?)
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:AlHal2
Comment Utility
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"/>
0
 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 500 total points
Comment Utility
ok sorry.. i stole the code from my app that uses CheckBoxList, hence the Selected property.

for my code should be
       
cblst.Checked = checkState;

Open in new window


to add the event handler, I always select the control on the Display tab, then in the properties window select 'events' icon and then double click the event I want.  This creates the event handler with all the correct properties.

have you set breakpoint on the event handler ?
0
 

Author Closing Comment

by:AlHal2
Comment Utility
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now