Solved

Allow users to select all checkboxes

Posted on 2016-10-11
6
99 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 81

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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

740 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