0

I want to select all row of all pages in asp.net grid view when select all check box is checked using jquery,my code selecting the value of only first page.

<script type="text/javascript">

           function SelectAllCheckboxes(chk) {
                debugger;

                $("[id*=chkSelectAll]").live("click", function () {
                    var chkHeader = $(this);
                    $("#<%=gvUnPaidInvoiceDetail.ClientID%> tr").each(function () {
                        var grid = $(this).closest("table");

                        $("[id*=chkSelect]", grid).each(function () {
                            if (chkHeader.is(":checked")) {
                                $(this).attr("checked", "checked");

                            } else {
                                $(this).removeAttr("checked");

                            }
                        });
                    });
                });
            }
        </script>

<asp:TemplateField HeaderText="Select">
  <HeaderTemplate>
   <asp:CheckBox ID="chkSelectAll" runat="server" Text="" onclick="SelectAllCheckboxes(this);" />
 </HeaderTemplate>

 <ItemTemplate>
   <asp:CheckBox ID="chkSelect" runat="server" />
 </ItemTemplate>
</asp:TemplateField>
Ajay2707
  • 5,345
  • 6
  • 34
  • 52
Adesh Gaonkar
  • 31
  • 1
  • 1
  • 12

4 Answers4

1

use this js for all checkbox check and uncheck

$(document).ready(function () {
$('#MainContent_repCustomers_chkCheckAll').click(function () {
    if ($(this).is(':checked')) {
        $("div#customer table tbody tr td 
            input:[type='checkbox']:not(:checked)").each(function () {
            $(this).attr('checked', true);
        });
    }
    else {

        $("div#customer table tbody tr td 
                input:[type='checkbox']:checked").each(function () {
            $(this).attr('checked', false); 
        });
    }
});
});
Genish Parvadia
  • 1,343
  • 2
  • 17
  • 28
0

As you have used paging, so at any time only 10 records (pagesize of grid) exist.

So when you go to 2nd page, its show the 2nd page, but actually in the grid it have only from 11 to 20 actual records.

So if you want to get all other page's data to select eigher remove paging or you have to maintain the click of record in javascript variable as a array.

Without paging is good, you just put your grid into div and give the style or by css as

<div stype ='overflow-x=auto; height: 500px'> <your asp Grid> </div>

If you want with paging create a array as :

How can I create a two dimensional array in JavaScript?

On click store the id with add or remove from array.

For add: http://www.w3schools.com/jsref/jsref_splice.asp

For remove :How do I remove a particular element from an array in JavaScript?

Community
  • 1
  • 1
Ajay2707
  • 5,345
  • 6
  • 34
  • 52
0

No need to check for each tr on client side, as you want all data whenever user click on header checkbox.

   <asp:CheckBox ID="chkSelectAll" runat="server" Text=""   
    AutoPostBack="true" OnCheckedChanged="chkSelectAll_CheckedChanged"  />

protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
{
    CheckBox ChkBoxHeader = (CheckBox)Grd.HeaderRow.FindControl("chkSelectAll");
    if (ChkBoxHeader.Checked)
     {
          // Bind Your gridview without pagination
          GridView1.AllowPaging = False;
          BindGridView();
          GridView1.AllowPaging = True;
     }


}

public void   BindGridView(){
     // code to bind gridview
}
Satinder singh
  • 9,340
  • 15
  • 53
  • 93
  • @AdeshGaonkar: I don't know whether it's good coding or bad coding, but with paging you won't able to get all Rows that's am sure. You can do a trick like 1st enable paging then after bind disable Paging check my updated answer – Satinder singh May 25 '15 at 10:29
  • but when grid rebinds the check box become unchechked – Adesh Gaonkar May 25 '15 at 10:59
0
<asp:TemplateField HeaderText="Select">
  <HeaderTemplate>
   <asp:CheckBox ID="chkSelectAll" runat="server" Text="" onclick="SelectAllCheckboxes(this);" />
   <!--I am not sure if above function called. You might need onClientClick
   insted of onclick-->
 </HeaderTemplate>

 <ItemTemplate>
   <asp:CheckBox ID="chkSelect" cssClass='chkSelect' runat="server" />
   <!-- I made a change to add cssClass attribute because using single ID 
    for multiple elements is not a good approach use class instead-->
 </ItemTemplate>
</asp:TemplateField>

<script type="text/javascript">
function SelectAllCheckboxes()
{
    alert("called");
    // if you want to really check/uncheck all then simply

    //$('.chkSelect').attr("checked","checked"); and
    //$('.chkSelect').removeAttr("checked");

    // But it seems you want to toggle so
     var chekedBoxes = $('.chkSelect:checked');
     var unchekedBoxes = $('.chkSelect:not(:checked)');
     chekedBoxes.removeAttr("checked");
     unchekedBoxes.attr("checked", "checked");        
}
</script>
Sami
  • 7,326
  • 6
  • 59
  • 91
  • $('.chkSelect').each(i, function() u r calling i in function where u have declared it and wht is the use of tht – Adesh Gaonkar May 26 '15 at 07:21
  • I wasn't wrong here but useless.. I kept only non iterative solution. If you follow the comments in post hopefully you will get it fixed.. Once you really reach in this function.. mean alert works – Sami May 27 '15 at 13:22