Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everybody, im new here and a dont have the best inglish, I will do my best to make me understand

now, i have a litle problem with my gridview.
im trying to put 4 columns with checkboxs into my grdiview, each colum has a unique checkbox in the header and im try to make that header checkbox a "selectAll checkbox"... 1 for each colum

i have this code in the first checkbox colum.. when i select this checkbox, each checkbox in every colum was checked, what can i do?

JavaScript
 <script language="javascript" type="text/javascript">
       function SelectAll(Id) {
           var myform = document.forms[0];
           var len = myform.elements.length;

           document.getElementById(Id).checked == true ? document.getElementById(Id).checked = false : document.getElementById(Id).checked = true;

           for (var i = 0; i < len; i++) {
               if (myform.elements[i].type == 'checkbox') {
                   if (myform.elements[i].checked) {
                       myform.elements[i].checked = false;
                   }
                   else {
                       myform.elements[i].checked = true;
                   }
               }
           }
       }
</script>
Posted
Comments
Karthik. A 20-Mar-12 20:53pm    
For such tasks, jquery is the way to go! I am not saying you should not do this in pure javascript, but in case you need a solution for this in jquery you would really get a lot of answers, me included! if you are unaware of jquery, have a look at it!

1 solution

hey
try this.
C#
function CheckAll(checkboxId, index) {
            var grid = document.getElementById('GridView1');

            if (grid.rows.length > 0) {
                //loop starts from 1. rows[0] points to the header.
                for (i = 1; i < grid.rows.length; i++) {
                    //get the reference of first column
                    if (index > 0) {
                        cell = grid.rows[i].cells[index];
                        for (j = 0; j < cell.childNodes.length; j++) {
                            //if childNode type is CheckBox
                            if (cell.childNodes[j].type == "checkbox") {
                                //assign the status of the Select All checkbox to the cell checkbox within the grid
                                cell.childNodes[j].checked = checkboxId.checked;
                            }
                        }

                    }
                }
            }
        }


//Calling of function

XML
<HeaderTemplate>
                                                                                <asp:CheckBox ID="chkHeader" Text="Check" runat="server" ToolTip="Check All" onclick="return CheckAll(chkHeader,columnNo);" />
                                                                            </HeaderTemplate>


This works for me
try and give replay
Best Luck
 
Share this answer
 
v3
Comments
FelipeVazquez 21-Mar-12 12:56pm    
thanks for your answers,
Nilesh i use your code, but isnt work for me
i have this... can you tell me if I'm doing something wrong

<pre lang="Javascript">
<script language="javascript" type="text/javascript">
function CheckAll(checkboxId, index) {
var grid = document.getElementById('gvDatos0');

if (grid.rows.length > 0) {
//loop starts from 1. rows[0] points to the header.
for (i = 1; i < grid.rows.length; i++) {
//get the reference of first column
if (index > 0) {
cell = grid.rows[i].cells[index];
for (j = 0; j < cell.childNodes.length; j++) {
//if childNode type is CheckBox
if (cell.childNodes[j].type == "checkbox") {
//assign the status of the Select All checkbox to the cell checkbox within the grid
cell.childNodes[j].checked = checkboxId.checked;
}
}

}
}
}
}
</script>

</pre>

<pre lang="HTML">
<asp:GridView ID="gvDatos0" runat="server" AutoGenerateColumns="False"
HorizontalAlign="Center" Width="850px">
<columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkHeader" runat="server" ToolTip="Check All" onclick="return CheckAll(chkHeader,columnNo);" />

</HeaderTemplate>
<itemtemplate>
<asp:CheckBox ID="chbMercado" runat="server" class="chkSel" />

</pre>
Nilesh Patil Kolhapur 26-Mar-12 0:30am    
column no means ur cell no i.e- 1,2,3,4 .........

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



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