hi,

how do we select multiple checkboxes in datagrid using a checkbox in the header of that datagrid. I went successful in adding each check boxes to each row. Now insted of selecting each checkbox individually i used a checkbox in the header of that datagrid. So when i check this, all the checkboxes in my datagrid should get selected and when i deselect the reverse should happen. How can i do this. I tried it this way

var checkflag="false";          var re;         function checkall(aspCheckBoxID, checkVal)          {           re = new RegExp(':' + aspCheckBoxID + '$');         for(i = 0; document.forms[0].elements.length; i++)          {                elm = document.forms[0].elements[i];               if (elm.type == 'checkbox')                   {                 if (re.test(elm.name))                     {                          elm.checked = checkVal;                        }                }         }               }

Recommended Answers

All 7 Replies

I'm not sure this document.forms[0].elements gets all the nested child elements and the checkboxes will be nested down in table/tr/td dom elements. try var elements = document.getElementsByTagName("input"); and iterate that instead.

just try this

this vl solve the problem


function checkAll(o) { for (var i = 0; i < document.Form1.elements.length; i++) { if (document.Form1.elements.type == "checkbox") { document.Form1.elements.checked = o.checked; } } }

kapil.goyal: just try this

this vl solve the problem

 function checkAll(o) { for (var i = 0; i < document.Form1.elements.length; i++) { if (document.Form1.elements[i].type == "checkbox") { document.Form1.elements[i].checked = o.checked; } } }

hi kapil,
i tried but i couldn't get the solution. Can you excatly tell me the process from the beginning, so that i can check whether i have made any mistake or not. Like, from adding the checkbox into the datagrid till the selection. Please

just follow this process

create a template column and add this code to tat

<HeaderTemplate>
<input runat="server" type="checkbox" ID="chkSelectAll" title="Select All Files" onclick="checkAll(this);" NAME="chkSelectAll" />
</HeaderTemplate>

and add the following code under HEAD tag of HTML code


<script language="javascript" type="text/javascript">
function checkAll(o) {
for (var i = 0; i < document.Form1.elements.length; i++) {
if (document.Form1.elements.type == "checkbox") {
document.Form1.elements.checked = o.checked;
}
}
}
</script>

kapil.goyal: just follow this process

create a template column and add this code to tat

 <HeaderTemplate>
 <input runat="server" type="checkbox" ID="chkSelectAll" title="Select All Files" onclick="checkAll(this);" NAME="chkSelectAll" />
 </HeaderTemplate>

and add the following code under HEAD tag of HTML code

 <script language="javascript" type="text/javascript">
 function checkAll(o) {
 for (var i = 0; i < document.Form1.elements.length; i++) {
 if (document.Form1.elements[i].type == "checkbox") {
 document.Form1.elements[i].checked = o.checked;
 }
 }
 }
 </script>

hi kapil,

I tried it another way and got the solution. Ikept a command button above my dgrid and wrote coding like if true then make it false and if false then make it true.But now the problem is all the checkbox's which are inside my datagrid are having the same name. So if i want to select some particular checkboxes inside my datagrid and do transactions for that particular checkboxes means, how should i select that. For example if i want to send some message to only some selected persons from the list means how can i do it

take a label in item template inside the template column containg header template.assign the field to label on which you want to perform operation like primary key.and make label visible false.and access the check box by label value.

take a label in item template inside the template column containg header template.assign the field to label on which you want to perform operation like primary key.and make label visible false.and access the check box by label value.

hi kapil,

This is little bit confusing. Can you be little more clear. Actually i want to send message to the selected user's in my datagrid. My datagrid consists of three columns. The first one consists of a checkbox, the second and third consists of label's. So i want to get the value that is in the third column for all the checked chkbox in my first column. So How to get the value for the checked chkboxes.

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.