Solved

Allow users to select all checkboxes

Posted on 2016-10-11
6
85 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 80

Expert Comment

by:David Johnson, CD, MVP
ID: 41839300
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)
ID: 41839623
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
ID: 41841513
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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:AlHal2
ID: 41841521
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
ID: 41841788
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
ID: 41841794
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

828 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