Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Allow users to select all checkboxes

Posted on 2016-10-11
6
Medium Priority
?
128 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 83

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

596 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