Check box functionality [Resolved]

Posted by Nagukothapalli under JavaScript on 1/30/2013 | Points: 10 | Views : 1137 | Status : [Member] | Replies : 1
Hi i had a gridview with check box as a column ,if i check the header text then all rows should be checked and if i unchecked the child check box header check box should be unchecked please post some sample




Responses

Posted by: Saratvaddilli on: 1/31/2013 [Member] [MVP] Bronze | Points: 50

Up
0
Down

Resolved
Hi use this on it worked fine for me


<script type="text/javascript">
<!--
function Check_Click(objRef) {
//Get the Row based on checkbox
var row = objRef.parentNode.parentNode;

//Get the reference of GridView
var GridView = row.parentNode;

//Get all input elements in Gridview
var inputList = GridView.getElementsByTagName("input");

for (var i = 0; i < inputList.length; i++) {
//The First element is the Header Checkbox
var headerCheckBox = inputList[0];

//Based on all or none checkboxes
//are checked check/uncheck Header Checkbox
var checked = true;
if (inputList[i].type == "checkbox" && inputList[i] != headerCheckBox) {
if (!inputList[i].checked) {
checked = false;
break;
}
}
}
headerCheckBox.checked = checked;

}
function checkAll(objRef) {
var GridView = objRef.parentNode.parentNode.parentNode;
var inputList = GridView.getElementsByTagName("input");
for (var i = 0; i < inputList.length; i++) {
var row = inputList[i].parentNode.parentNode;
if (inputList[i].type == "checkbox" && objRef != inputList[i]) {
if (objRef.checked) {
inputList[i].checked = true;
}
else {
if (row.rowIndex % 2 == 0) {
row.style.backgroundColor = "#C2D69B";
}
else {
row.style.backgroundColor = "white";
}
inputList[i].checked = false;
}
}
}
}
//-->
</script>


In the Item template and header Template

 <HeaderTemplate>

<asp:CheckBox ID="chkAll" runat="server" onclick="checkAll(this);" AutoPostBack="true"
OnCheckedChanged="CheckBox_CheckChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" onclick="Check_Click(this)" AutoPostBack="true"
OnCheckedChanged="CheckBox_CheckChanged" />
</ItemTemplate>



Hope this was helpful for you

Thanks and Regards
V.SaratChand
Show difficulties that how difficult you are

Nagukothapalli, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response