HI i have 4 gridview in one form...i have to check all text field if someone select main check box...
my code is working only on one grid view..its not working on other grid view.
what should i do?
my code is

<script type="text/javascript">

 function checkAll(chkid) 
    {
        var chk = document.getElementById(chkid);



        var allchk = document.getElementsByName("contacts");
        for (var i = 0; i < allchk.length; i++) {
            allchk[i].checked = chk.checked;
        }


    }




 </script>

Regards..
Farhad

Dani AI

Generated

The core problem in the thread is scoping. 's handler grabs elements from the whole document, and with multiple GridViews that can mean either the wrong set of checkboxes or none at all (ASP.NET often changes rendered names/ids). 's snippet contains DOM-method typos, and 's routine simply toggles every checkbox in a form instead of setting row checkboxes to the master checkbox's state. A reliable pattern is: mark row checkboxes with a class (or give the GridView a known container id), give each master checkbox a reference to its grid (data attribute), and then scope the selection to that container so each master affects only its grid.

Example markup + vanilla JS (safe, avoids server-ID issues):

<!-- master placed above each grid -->
<input type="checkbox" class="master-check" data-target="#Grid1">

<!-- row checkboxes inside the grid rows -->
<input type="checkbox" class="row-check">

<script>
document.addEventListener('DOMContentLoaded', function () {
  var masters = document.querySelectorAll('.master-check');
  for (var i = 0; i < masters.length; i++) {
    masters[i].addEventListener('change', function () {
      var target = this.getAttribute('data-target');
      var container = target ? document.querySelector(target) : this.closest('.grid-container');
      if (!container) return;
      var rows = container.querySelectorAll('input[type="checkbox"].row-check');
      for (var j = 0; j < rows.length; j++) rows[j].checked = this.checked;
    });
  }
});
</script>

Troubleshooting notes: ensure the row checkboxes actually have the class used by the script; bind after DOM ready; prefer change events not click; for ASP.NET GridView add CssClass="row-check" on the CheckBox in the TemplateField (or set ClientIDMode="Static") so client-side selectors work; for older browsers replace closest/forEach with simple loops as shown. This approach keeps each master checkbox scoped to its grid and avoids name/ID collisions across multiple GridViews.

Recommended Answers

All 2 Replies

Member Avatar for Member #949455

i have to check all text field if someone select main check box...

You can try this (I test it on my end and it works fine the only difference is that you provided a different var than mines, so keep that in mind):

<script type="text/javascript">
    function checkedAll () {
        var chk = document.getElementsById('chkid');
        var checkboxes = [];
        var check = document.getElementByName('contacts').checked
        for (var i = 0; i < allchk.length; i++) {
            if (chkid[i].type == 'checkbox') {
                chkid[i].checked = check;
            }
        }
    }
</script>

try this common function to check or uncheck input checkbox from 'formname' form

 function checkAll(){
        var inputTags = formname.getElementsByTagName('input'); 

        for (var i=0, length = inputTags.length; i<length; i++) {
             if (inputTags[i].type == 'checkbox') {
                 if(inputTags[i].checked==true){
                    inputTags[i].checked = false;
                 }else{

                    inputTags[i].checked = true;
                 }
             }
        }

     }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.