Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all.In my web page i've a GridViewwith some columns.In those columns i've a Status(Is Active or Not i.e. 1 or 0 if the data value for status col is 1 then the CheckBox will be checked and if the value is 0 the CheckBox will remain unchecked) column(TemplateField) with CheckBox in ItemTemplate. Now i also have another TemplateField with CheckBoxes in HeaderTemplate(for check all functionality) and in ItemTemplate(for individual row selection purpose).I've written JavaScript for CheckAll CheckBox functionality.Now the problem is when i'm checking the CheckAll Cehckbox the Status column checkboxes are also getting checked which is wrong in my requirement.Kindly help me in this scenario.Here I'm pasting my Source Code.Please go through it.

Thanks in Advance

JavaScript
<script type="text/javascript" language="javascript">

function toggleCheckBoxes(gvId, isChecked) 
{

var checkboxes = getCheckBoxesFrom(document.getElementById(gvId));

for (i = 0; i <= checkboxes.length - 1; i++) {

checkboxes[i].checked = isChecked; 
}

}
function getCheckBoxesFrom(gv) 
{

var checkboxesArray = new Array(); 
var inputElements = gv.getElementsByTagName("input");
if (inputElements.length == 0) null;

for (i = 0; i <= inputElements.length -1; i++) {

if (isCheckBox(inputElements[i])) {

checkboxesArray.push(inputElements[i]); 
}

}

return checkboxesArray; 
}
function isCheckBox(element) 
{
return element.type == "checkbox"; 
}
</script>


ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<columns>
<asp:TemplateField HeaderText="Sl.No">
<itemtemplate>
<%#Container.DataItemIndex + 1 %>
</itemtemplate>

<asp:BoundField DataField="id" HeaderText="ID" />
<asp:TemplateField HeaderText="Name">
<itemtemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>'>
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("name") %>'>
</edititemtemplate>

<asp:TemplateField HeaderText="Salary">
<itemtemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("sal") %>'>
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("sal") %>'>
</edititemtemplate>

<asp:TemplateField HeaderText="Status">
<itemtemplate>
<asp:CheckBox ID="chkStatus" runat="server" Checked='<%#Convert.ToBoolean(Eval("status"))%>' />
</itemtemplate>

<asp:TemplateField>
<HeaderTemplate>
<input 
type="checkbox" 
 önclick="toggleCheckBoxes('<%= GridView1.ClientID %>',this.checked)" 
id="chkAll" />
</HeaderTemplate>
<itemtemplate>
<asp:CheckBox ID="chkSingle" runat="server" />
</itemtemplate>

</columns>
Posted
Updated 26-Mar-13 1:47am
v2
Comments
Vani Kulkarni 26-Mar-13 7:47am    
[Edit] Formatted Code [/Edit]

Hello friend,
please try this function

C#
function SelectAll() {
  $("[id*='chkSingle']").attr("checked", $("[id*='chkAll'").attr("checked"));
}
 
Share this answer
 
You have to check that the checkbox you want to check or uncheck is the same that has id "chkSingle". For this add another line:
JavaScript
var chkID = "chkSingle";

This way only those checkboxes wil be added to the array that have "chkSingle" in their id's.
JavaScript
inputElements[i].id.indexOf(chkID) != -1


JavaScript
function getCheckBoxesFrom(gv) 
{ 
var checkboxesArray = new Array(); 
var inputElements = gv.getElementsByTagName("input");
var chkID = "chkSingle";
if (inputElements.length == 0) null; 
for (i = 0; i <= inputElements.length -1; i++) { 
if (isCheckBox(inputElements[i]) && (inputElements[i].id.indexOf(chkID) != -1)) { 
checkboxesArray.push(inputElements[i]); 
} 
}
 
Share this answer
 
Comments
Jagan911 28-Mar-13 1:36am    
Thank You Zafar Sultan.Your answer was quite working well.
Zafar Sultan 28-Mar-13 3:56am    
Welcome.

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