Checking a Checkbox (in header template) in Gridview and other Chekcboxes (in item templates) get checked as...












0















I want when I check the checkbox in the header template, then other checkboxes in the Item Template should get checked as well Unfortunately this is not working as expected. I added script referencing a the jquery version 1.6.2 in the head tag of the HTML then a function that which will be fired after the checked box is clicked. Also on the Checkbox in the Header Template, the onclick function has the jquery function which I wrote in the at the head tag. The Alert is working fine. I did this to help me test that Jquery is working. Notice no AutoPostBack attributes But the Toggling is not working.



//this script is in the head tag
function toggleSelectionUsingHeaderCheckBox(source) {
$("#GridView1 input[name$='cbDelete']").each(function () {
$(this).attr('checked', source.checked);
});
//alert('checked');
//the alert above is working fine
}


the Gridview code from columns



<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="toggleSelectionUsingHeaderCheckBox(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblEmployeeId" runat="server"
Text='<%# Bind("EmployeeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="StaffName" HeaderText="Name" />
<asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
<asp:BoundField DataField="LockStatus" HeaderText="Status" />
</Columns>









share|improve this question

























  • Check out the link below whether it answers your question forums.asp.net/t/…

    – muhdamean
    Dec 31 '18 at 14:33











  • You don't need that ASP code to troubleshoot this; you just need a simple HTML page. Get it working that way first, then adapt your solution to ASP.

    – Robert Harvey
    Dec 31 '18 at 17:28
















0















I want when I check the checkbox in the header template, then other checkboxes in the Item Template should get checked as well Unfortunately this is not working as expected. I added script referencing a the jquery version 1.6.2 in the head tag of the HTML then a function that which will be fired after the checked box is clicked. Also on the Checkbox in the Header Template, the onclick function has the jquery function which I wrote in the at the head tag. The Alert is working fine. I did this to help me test that Jquery is working. Notice no AutoPostBack attributes But the Toggling is not working.



//this script is in the head tag
function toggleSelectionUsingHeaderCheckBox(source) {
$("#GridView1 input[name$='cbDelete']").each(function () {
$(this).attr('checked', source.checked);
});
//alert('checked');
//the alert above is working fine
}


the Gridview code from columns



<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="toggleSelectionUsingHeaderCheckBox(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblEmployeeId" runat="server"
Text='<%# Bind("EmployeeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="StaffName" HeaderText="Name" />
<asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
<asp:BoundField DataField="LockStatus" HeaderText="Status" />
</Columns>









share|improve this question

























  • Check out the link below whether it answers your question forums.asp.net/t/…

    – muhdamean
    Dec 31 '18 at 14:33











  • You don't need that ASP code to troubleshoot this; you just need a simple HTML page. Get it working that way first, then adapt your solution to ASP.

    – Robert Harvey
    Dec 31 '18 at 17:28














0












0








0


1






I want when I check the checkbox in the header template, then other checkboxes in the Item Template should get checked as well Unfortunately this is not working as expected. I added script referencing a the jquery version 1.6.2 in the head tag of the HTML then a function that which will be fired after the checked box is clicked. Also on the Checkbox in the Header Template, the onclick function has the jquery function which I wrote in the at the head tag. The Alert is working fine. I did this to help me test that Jquery is working. Notice no AutoPostBack attributes But the Toggling is not working.



//this script is in the head tag
function toggleSelectionUsingHeaderCheckBox(source) {
$("#GridView1 input[name$='cbDelete']").each(function () {
$(this).attr('checked', source.checked);
});
//alert('checked');
//the alert above is working fine
}


the Gridview code from columns



<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="toggleSelectionUsingHeaderCheckBox(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblEmployeeId" runat="server"
Text='<%# Bind("EmployeeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="StaffName" HeaderText="Name" />
<asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
<asp:BoundField DataField="LockStatus" HeaderText="Status" />
</Columns>









share|improve this question
















I want when I check the checkbox in the header template, then other checkboxes in the Item Template should get checked as well Unfortunately this is not working as expected. I added script referencing a the jquery version 1.6.2 in the head tag of the HTML then a function that which will be fired after the checked box is clicked. Also on the Checkbox in the Header Template, the onclick function has the jquery function which I wrote in the at the head tag. The Alert is working fine. I did this to help me test that Jquery is working. Notice no AutoPostBack attributes But the Toggling is not working.



//this script is in the head tag
function toggleSelectionUsingHeaderCheckBox(source) {
$("#GridView1 input[name$='cbDelete']").each(function () {
$(this).attr('checked', source.checked);
});
//alert('checked');
//the alert above is working fine
}


the Gridview code from columns



<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="toggleSelectionUsingHeaderCheckBox(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblEmployeeId" runat="server"
Text='<%# Bind("EmployeeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="StaffName" HeaderText="Name" />
<asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
<asp:BoundField DataField="LockStatus" HeaderText="Status" />
</Columns>






javascript c# jquery asp.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 17:17









VDWWD

23.6k113651




23.6k113651










asked Dec 31 '18 at 14:05









Hassan SalihuHassan Salihu

83




83













  • Check out the link below whether it answers your question forums.asp.net/t/…

    – muhdamean
    Dec 31 '18 at 14:33











  • You don't need that ASP code to troubleshoot this; you just need a simple HTML page. Get it working that way first, then adapt your solution to ASP.

    – Robert Harvey
    Dec 31 '18 at 17:28



















  • Check out the link below whether it answers your question forums.asp.net/t/…

    – muhdamean
    Dec 31 '18 at 14:33











  • You don't need that ASP code to troubleshoot this; you just need a simple HTML page. Get it working that way first, then adapt your solution to ASP.

    – Robert Harvey
    Dec 31 '18 at 17:28

















Check out the link below whether it answers your question forums.asp.net/t/…

– muhdamean
Dec 31 '18 at 14:33





Check out the link below whether it answers your question forums.asp.net/t/…

– muhdamean
Dec 31 '18 at 14:33













You don't need that ASP code to troubleshoot this; you just need a simple HTML page. Get it working that way first, then adapt your solution to ASP.

– Robert Harvey
Dec 31 '18 at 17:28





You don't need that ASP code to troubleshoot this; you just need a simple HTML page. Get it working that way first, then adapt your solution to ASP.

– Robert Harvey
Dec 31 '18 at 17:28












2 Answers
2






active

oldest

votes


















0














You just have to loop through all the rows in the gridview, get the checkbox for each row and mark it as checked as shown below.



<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="toggleSelectionUsingHeaderCheckBox(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblEmployeeId" runat="server"
Text='<%# Bind("EmployeeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="StaffName" HeaderText="Name" />
<asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
<asp:BoundField DataField="LockStatus" HeaderText="Status" />
</Columns>


Then javascript



<script>
function toggleSelectionUsingHeaderCheckBox(chkbx) {
var GridView1 = document.getElementById("<%=GridView1.ClientID %>");
for (i = 1; i < GridView1.rows.length; i++) {
GridView1.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = chkbx.checked;
}
}
</script>


The checkbox in the gridview header calls the javascript function which does the looping and checking.



Hope this helps.






share|improve this answer































    0














    This finishes the job for me



    var gridViewId = '#<%= GridView1.ClientID %>';
    function checkAll(selectAllCheckbox) {
    //get all checkboxes within item rows and select/deselect based on select all checked status
    //:checkbox is jquery selector to get all checkboxes
    $('td :checkbox', gridViewId).prop("checked", selectAllCheckbox.checked);
    }
    function unCheckSelectAll(selectCheckbox) {
    //if any item is unchecked, uncheck header checkbox as well
    if (!selectCheckbox.checked)
    $('th :checkbox', gridViewId).prop("checked", false);
    }


    To count number of items checked, use this before performing an action on your button



    $(document).ready(function () {
    $('#<%=btnDelete.ClientID%>').click(function () {

    var gv = document.getElementById("<%= GridView1.ClientID %>");
    var inputList = gv.getElementsByTagName("input");
    var numChecked = 0;

    for (var i = 0; i < inputList.length; i++) {
    if (inputList[i].type == "checkbox" && inputList[i].checked) {
    numChecked = numChecked + 1;
    }
    }
    if (numChecked == 0) {
    alert('No rows selected');
    return false;
    }
    else {
    return confirm(numChecked + ' row(s) will be locked or deleted');
    }
    });
    });


    then the html markup



    <Columns>
    <asp:TemplateField>
    <HeaderTemplate>
    <asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="checkAll(this);" />
    </HeaderTemplate>
    <ItemTemplate>
    <asp:CheckBox ID="cbDelete" onclick="unCheckSelectAll(this);" runat="server" />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Id">
    <ItemTemplate>
    <asp:Label ID="lblEmployeeId" runat="server"
    Text='<%# Bind("EmployeeId") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="StaffName" HeaderText="Name" />
    <asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
    <asp:BoundField DataField="LockStatus" HeaderText="Status" />
    </Columns>





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988347%2fchecking-a-checkbox-in-header-template-in-gridview-and-other-chekcboxes-in-it%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You just have to loop through all the rows in the gridview, get the checkbox for each row and mark it as checked as shown below.



      <Columns>
      <asp:TemplateField>
      <HeaderTemplate>
      <asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="toggleSelectionUsingHeaderCheckBox(this);" />
      </HeaderTemplate>
      <ItemTemplate>
      <asp:CheckBox ID="cbDelete" runat="server" />
      </ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField HeaderText="Id">
      <ItemTemplate>
      <asp:Label ID="lblEmployeeId" runat="server"
      Text='<%# Bind("EmployeeId") %>'></asp:Label>
      </ItemTemplate>
      </asp:TemplateField>
      <asp:BoundField DataField="StaffName" HeaderText="Name" />
      <asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
      <asp:BoundField DataField="LockStatus" HeaderText="Status" />
      </Columns>


      Then javascript



      <script>
      function toggleSelectionUsingHeaderCheckBox(chkbx) {
      var GridView1 = document.getElementById("<%=GridView1.ClientID %>");
      for (i = 1; i < GridView1.rows.length; i++) {
      GridView1.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = chkbx.checked;
      }
      }
      </script>


      The checkbox in the gridview header calls the javascript function which does the looping and checking.



      Hope this helps.






      share|improve this answer




























        0














        You just have to loop through all the rows in the gridview, get the checkbox for each row and mark it as checked as shown below.



        <Columns>
        <asp:TemplateField>
        <HeaderTemplate>
        <asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="toggleSelectionUsingHeaderCheckBox(this);" />
        </HeaderTemplate>
        <ItemTemplate>
        <asp:CheckBox ID="cbDelete" runat="server" />
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Id">
        <ItemTemplate>
        <asp:Label ID="lblEmployeeId" runat="server"
        Text='<%# Bind("EmployeeId") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="StaffName" HeaderText="Name" />
        <asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
        <asp:BoundField DataField="LockStatus" HeaderText="Status" />
        </Columns>


        Then javascript



        <script>
        function toggleSelectionUsingHeaderCheckBox(chkbx) {
        var GridView1 = document.getElementById("<%=GridView1.ClientID %>");
        for (i = 1; i < GridView1.rows.length; i++) {
        GridView1.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = chkbx.checked;
        }
        }
        </script>


        The checkbox in the gridview header calls the javascript function which does the looping and checking.



        Hope this helps.






        share|improve this answer


























          0












          0








          0







          You just have to loop through all the rows in the gridview, get the checkbox for each row and mark it as checked as shown below.



          <Columns>
          <asp:TemplateField>
          <HeaderTemplate>
          <asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="toggleSelectionUsingHeaderCheckBox(this);" />
          </HeaderTemplate>
          <ItemTemplate>
          <asp:CheckBox ID="cbDelete" runat="server" />
          </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Id">
          <ItemTemplate>
          <asp:Label ID="lblEmployeeId" runat="server"
          Text='<%# Bind("EmployeeId") %>'></asp:Label>
          </ItemTemplate>
          </asp:TemplateField>
          <asp:BoundField DataField="StaffName" HeaderText="Name" />
          <asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
          <asp:BoundField DataField="LockStatus" HeaderText="Status" />
          </Columns>


          Then javascript



          <script>
          function toggleSelectionUsingHeaderCheckBox(chkbx) {
          var GridView1 = document.getElementById("<%=GridView1.ClientID %>");
          for (i = 1; i < GridView1.rows.length; i++) {
          GridView1.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = chkbx.checked;
          }
          }
          </script>


          The checkbox in the gridview header calls the javascript function which does the looping and checking.



          Hope this helps.






          share|improve this answer













          You just have to loop through all the rows in the gridview, get the checkbox for each row and mark it as checked as shown below.



          <Columns>
          <asp:TemplateField>
          <HeaderTemplate>
          <asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="toggleSelectionUsingHeaderCheckBox(this);" />
          </HeaderTemplate>
          <ItemTemplate>
          <asp:CheckBox ID="cbDelete" runat="server" />
          </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Id">
          <ItemTemplate>
          <asp:Label ID="lblEmployeeId" runat="server"
          Text='<%# Bind("EmployeeId") %>'></asp:Label>
          </ItemTemplate>
          </asp:TemplateField>
          <asp:BoundField DataField="StaffName" HeaderText="Name" />
          <asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
          <asp:BoundField DataField="LockStatus" HeaderText="Status" />
          </Columns>


          Then javascript



          <script>
          function toggleSelectionUsingHeaderCheckBox(chkbx) {
          var GridView1 = document.getElementById("<%=GridView1.ClientID %>");
          for (i = 1; i < GridView1.rows.length; i++) {
          GridView1.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = chkbx.checked;
          }
          }
          </script>


          The checkbox in the gridview header calls the javascript function which does the looping and checking.



          Hope this helps.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 31 '18 at 22:11









          Ahmad TijaniAhmad Tijani

          1428




          1428

























              0














              This finishes the job for me



              var gridViewId = '#<%= GridView1.ClientID %>';
              function checkAll(selectAllCheckbox) {
              //get all checkboxes within item rows and select/deselect based on select all checked status
              //:checkbox is jquery selector to get all checkboxes
              $('td :checkbox', gridViewId).prop("checked", selectAllCheckbox.checked);
              }
              function unCheckSelectAll(selectCheckbox) {
              //if any item is unchecked, uncheck header checkbox as well
              if (!selectCheckbox.checked)
              $('th :checkbox', gridViewId).prop("checked", false);
              }


              To count number of items checked, use this before performing an action on your button



              $(document).ready(function () {
              $('#<%=btnDelete.ClientID%>').click(function () {

              var gv = document.getElementById("<%= GridView1.ClientID %>");
              var inputList = gv.getElementsByTagName("input");
              var numChecked = 0;

              for (var i = 0; i < inputList.length; i++) {
              if (inputList[i].type == "checkbox" && inputList[i].checked) {
              numChecked = numChecked + 1;
              }
              }
              if (numChecked == 0) {
              alert('No rows selected');
              return false;
              }
              else {
              return confirm(numChecked + ' row(s) will be locked or deleted');
              }
              });
              });


              then the html markup



              <Columns>
              <asp:TemplateField>
              <HeaderTemplate>
              <asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="checkAll(this);" />
              </HeaderTemplate>
              <ItemTemplate>
              <asp:CheckBox ID="cbDelete" onclick="unCheckSelectAll(this);" runat="server" />
              </ItemTemplate>
              </asp:TemplateField>
              <asp:TemplateField HeaderText="Id">
              <ItemTemplate>
              <asp:Label ID="lblEmployeeId" runat="server"
              Text='<%# Bind("EmployeeId") %>'></asp:Label>
              </ItemTemplate>
              </asp:TemplateField>
              <asp:BoundField DataField="StaffName" HeaderText="Name" />
              <asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
              <asp:BoundField DataField="LockStatus" HeaderText="Status" />
              </Columns>





              share|improve this answer




























                0














                This finishes the job for me



                var gridViewId = '#<%= GridView1.ClientID %>';
                function checkAll(selectAllCheckbox) {
                //get all checkboxes within item rows and select/deselect based on select all checked status
                //:checkbox is jquery selector to get all checkboxes
                $('td :checkbox', gridViewId).prop("checked", selectAllCheckbox.checked);
                }
                function unCheckSelectAll(selectCheckbox) {
                //if any item is unchecked, uncheck header checkbox as well
                if (!selectCheckbox.checked)
                $('th :checkbox', gridViewId).prop("checked", false);
                }


                To count number of items checked, use this before performing an action on your button



                $(document).ready(function () {
                $('#<%=btnDelete.ClientID%>').click(function () {

                var gv = document.getElementById("<%= GridView1.ClientID %>");
                var inputList = gv.getElementsByTagName("input");
                var numChecked = 0;

                for (var i = 0; i < inputList.length; i++) {
                if (inputList[i].type == "checkbox" && inputList[i].checked) {
                numChecked = numChecked + 1;
                }
                }
                if (numChecked == 0) {
                alert('No rows selected');
                return false;
                }
                else {
                return confirm(numChecked + ' row(s) will be locked or deleted');
                }
                });
                });


                then the html markup



                <Columns>
                <asp:TemplateField>
                <HeaderTemplate>
                <asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="checkAll(this);" />
                </HeaderTemplate>
                <ItemTemplate>
                <asp:CheckBox ID="cbDelete" onclick="unCheckSelectAll(this);" runat="server" />
                </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Id">
                <ItemTemplate>
                <asp:Label ID="lblEmployeeId" runat="server"
                Text='<%# Bind("EmployeeId") %>'></asp:Label>
                </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="StaffName" HeaderText="Name" />
                <asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
                <asp:BoundField DataField="LockStatus" HeaderText="Status" />
                </Columns>





                share|improve this answer


























                  0












                  0








                  0







                  This finishes the job for me



                  var gridViewId = '#<%= GridView1.ClientID %>';
                  function checkAll(selectAllCheckbox) {
                  //get all checkboxes within item rows and select/deselect based on select all checked status
                  //:checkbox is jquery selector to get all checkboxes
                  $('td :checkbox', gridViewId).prop("checked", selectAllCheckbox.checked);
                  }
                  function unCheckSelectAll(selectCheckbox) {
                  //if any item is unchecked, uncheck header checkbox as well
                  if (!selectCheckbox.checked)
                  $('th :checkbox', gridViewId).prop("checked", false);
                  }


                  To count number of items checked, use this before performing an action on your button



                  $(document).ready(function () {
                  $('#<%=btnDelete.ClientID%>').click(function () {

                  var gv = document.getElementById("<%= GridView1.ClientID %>");
                  var inputList = gv.getElementsByTagName("input");
                  var numChecked = 0;

                  for (var i = 0; i < inputList.length; i++) {
                  if (inputList[i].type == "checkbox" && inputList[i].checked) {
                  numChecked = numChecked + 1;
                  }
                  }
                  if (numChecked == 0) {
                  alert('No rows selected');
                  return false;
                  }
                  else {
                  return confirm(numChecked + ' row(s) will be locked or deleted');
                  }
                  });
                  });


                  then the html markup



                  <Columns>
                  <asp:TemplateField>
                  <HeaderTemplate>
                  <asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="checkAll(this);" />
                  </HeaderTemplate>
                  <ItemTemplate>
                  <asp:CheckBox ID="cbDelete" onclick="unCheckSelectAll(this);" runat="server" />
                  </ItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Id">
                  <ItemTemplate>
                  <asp:Label ID="lblEmployeeId" runat="server"
                  Text='<%# Bind("EmployeeId") %>'></asp:Label>
                  </ItemTemplate>
                  </asp:TemplateField>
                  <asp:BoundField DataField="StaffName" HeaderText="Name" />
                  <asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
                  <asp:BoundField DataField="LockStatus" HeaderText="Status" />
                  </Columns>





                  share|improve this answer













                  This finishes the job for me



                  var gridViewId = '#<%= GridView1.ClientID %>';
                  function checkAll(selectAllCheckbox) {
                  //get all checkboxes within item rows and select/deselect based on select all checked status
                  //:checkbox is jquery selector to get all checkboxes
                  $('td :checkbox', gridViewId).prop("checked", selectAllCheckbox.checked);
                  }
                  function unCheckSelectAll(selectCheckbox) {
                  //if any item is unchecked, uncheck header checkbox as well
                  if (!selectCheckbox.checked)
                  $('th :checkbox', gridViewId).prop("checked", false);
                  }


                  To count number of items checked, use this before performing an action on your button



                  $(document).ready(function () {
                  $('#<%=btnDelete.ClientID%>').click(function () {

                  var gv = document.getElementById("<%= GridView1.ClientID %>");
                  var inputList = gv.getElementsByTagName("input");
                  var numChecked = 0;

                  for (var i = 0; i < inputList.length; i++) {
                  if (inputList[i].type == "checkbox" && inputList[i].checked) {
                  numChecked = numChecked + 1;
                  }
                  }
                  if (numChecked == 0) {
                  alert('No rows selected');
                  return false;
                  }
                  else {
                  return confirm(numChecked + ' row(s) will be locked or deleted');
                  }
                  });
                  });


                  then the html markup



                  <Columns>
                  <asp:TemplateField>
                  <HeaderTemplate>
                  <asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="checkAll(this);" />
                  </HeaderTemplate>
                  <ItemTemplate>
                  <asp:CheckBox ID="cbDelete" onclick="unCheckSelectAll(this);" runat="server" />
                  </ItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Id">
                  <ItemTemplate>
                  <asp:Label ID="lblEmployeeId" runat="server"
                  Text='<%# Bind("EmployeeId") %>'></asp:Label>
                  </ItemTemplate>
                  </asp:TemplateField>
                  <asp:BoundField DataField="StaffName" HeaderText="Name" />
                  <asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
                  <asp:BoundField DataField="LockStatus" HeaderText="Status" />
                  </Columns>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 1 at 5:58









                  Hassan SalihuHassan Salihu

                  83




                  83






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988347%2fchecking-a-checkbox-in-header-template-in-gridview-and-other-chekcboxes-in-it%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas