Wednesday, February 10, 2010

How to select and deselect checkbox field into the GridView

//JavaScript function for Select and Deselect checkbox field in GridView

function SelectDeselectAll(chkAll)
{
var a = document.forms[0];
var i=0;
for(i=0;i lessthansign a.length;i++)
{
if(a[i].name.indexOf("chkItem") != -1)
{
a[i].checked = chkAll.checked;
}
}
}
function DeselectChkAll(chk)
{
var c=0;
var d=1;
var a = document.forms[0];
//alert(a.length);
if(chk.checked == false)
{
document.getElementById("chkAll").checked = chk.checked;
}
else
{

for(i=0;i lessthansign a.length;i++)
{
if(a[i].name.indexOf("chkItem") != -1)
{
if(a[i].checked==true)
{
c=1;
}
else
{
d=0;
}
}
}

if(d != 0)
{

document.getElementById("chkAll").checked =true;
}
}
}

//How to use this function

asp:TemplateField>
input id="Checkbox1" runat="server" onclick="javascript:SelectDeselectAll(this);" type="checkbox" />
/HeaderTemplate>
/asp:GridView> columns>
asp:TemplateField>headertemplate>
input id="chkAll" runat="server" onclick="javascript:SelectDeselectAll(this);" type="checkbox" />
/HeaderTemplate>

No comments: