Click here to Skip to main content
16,020,519 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am using the header checkbox in Template field of the grid.but when i click on header checkbox the other checkbox which is in row of grid not checked if it is not checked. i want to do that when header checkbox is cheked all other child checkbox will be checked and when header checkbox will be not checked or deselect the child checkbox also deselect.

i use :

 <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CCCCCC"
BorderStyle="None" BorderWidth="1px" CellPadding="10" CellSpacing="11" DataKeyNames="id" EmptyDataText="No Document Found for Selected category Width="100%" AutoGenerateColumns="False"              OnRowDataBound="GridView1_RowDataBound" EditIndex="0" CssClass="grid" 
nrowcreated="GridView1_RowCreated">
 <rowstyle forecolor="#000066" height="25px" />
 <columns>
              
 <asp:TemplateField HeaderText="ID" Visible="false" >
 <itemtemplate>
 <asp:Label ID="lbl_id" runat="server" Text='<%# Eval("id") %>'>
</itemtemplate>
                                                      
<HeaderStyle Width="10%" />
<itemstyle width="1%" />


 <asp:TemplateField HeaderText="Select Document">
<HeaderTemplate>
<asp:CheckBox ID="cbSelectAll" runat="server" onclick="javascript:SelectAllCheckboxes1(this);"  />
 </HeaderTemplate>
<itemtemplate>
 <asp:Checkbox ID="CheckBox1" runat="server" />          
                                                
  </itemtemplate>
 <itemstyle width="1%" />
 


i use the java script on design page:
<script type="text/javascript">
function SelectAllCheckboxes1(CheckBox1)
{
$('#<%=GridView1.ClientID%>').find("input:checkbox").each(function ()
 {
    if (this !=CheckBox1 ) 
    {
        this.checked = CheckBox1.checked;
    }
});
}</columns>
Posted

Hi ,
Check this Article
Selecting / Deselecting all the CheckBoxes Inside a GridView[^]
Best Regards
M.Mitwalli
 
Share this answer
 
Use This Java Script


C#
function SelectAll(id) {
         var frm = document.forms[0];

         for (i = 0; i < frm.elements.length; i++) {

             if (frm.elements[i].type == "checkbox") {

                 frm.elements[i].checked = document.getElementById(id).checked;

             }

         }


Use this Code For Check Box Inside gridview...
ASP.NET
<asp:templatefield >
<headertemplate>
<asp:checkbox id="chkSelectAll" runat="server" />
  </headertemplate>
 </asp:templatefield>



Add Row Data Bound Event and in .cs file call this javascript...

C#
protected void gdvTest_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.Header)
       {
           ((CheckBox)e.Row.FindControl("chkSelectAll")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("chkSelectAll")).ClientID + "')");

       }
   }
 
Share this answer
 
v2
Comments
[no name] 13-Jun-12 8:46am    
not working...
Tony Tom.k 13-Jun-12 9:26am    
wat error its showing.....
[no name] 14-Jun-12 0:15am    
there is no error showing but when click on header checkbox no effect is working on child checkbox.
Tony Tom.k 14-Jun-12 2:37am    
It will work boss I am using the same stuff in my project


check this event name gdvTest_RowDataBound is same in aspx and aspx.cs page...

put a brake point on gdvTest_RowDataBound event and check its entering into there on pageload,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900