I hope the following code will help you out to select all checkboxes in gridview in asp.net.
function SelectAllCheckboxesCategoryList(spanChk)
{
var chkALL = document.getElementById(spanChk);
var the_form = window.document.forms[0];
for(var i=0; i -1)
{
the_form.elements[i].checked = chkALL.checked;
}
}
}
}
function UncheckAllCategoryList(CheckItemClientID)
{
var chkItem = document.getElementById(CheckItemClientID);
var the_form = window.document.forms[0];
for(var i=0; i -1)
{
if(the_form.elements[i].checked = false)
the_form.elements[i].checked = chkItem.checked;
}
}
}
}
function ConfirmDelete()
{
var the_form = window.document.forms[0];
var flagDel=1;
for(var i=0; i -1)
{
if(the_form.elements[i].checked == true)
{
flagDel=0;
break;
}
}
}
}
if(flagDel==0)
{
var DialogRes = confirm(“Are you sure you want to delete records?”);
return DialogRes;
}
else
{
alert(“Must Select Atleast One Record To Delete.”);
return false;
}
}
Take one HeaderTemplate column and add one checkbox with ID=cbheaderCategory
Take one ItemTemplate column and add one checkbox with ID=chkDeleteCategory
Add following vb.net code in code behind file
Protected Sub dgrdCategory_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgrdCategory.ItemDataBound
If (e.Item.ItemType = ListItemType.Header) Then
Dim chk As CheckBox = CType(e.Item.Cells(0).FindControl(“cbheaderCategory”), CheckBox)
chk.Attributes.Add(“onclick”, “SelectAllCheckboxesCategoryList(‘” + chk.ClientID + “‘);”)
End If
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim chk As CheckBox = CType(e.Item.Cells(1).FindControl(“chkDeleteCategory”), CheckBox)
chk.Attributes.Add(“onclick”, “UncheckAllCategoryList(‘” + chk.ClientID + “‘);”)
End If
End Sub
To add event to check selected check boxes and confirmation message on delete button, use following code.
btnDelete.Attributes.Add(“OnClick”, “javascript: return ConfirmDelete();”)
I hope it helps you.
Thanks
Filed under: 1, Select All Checkboxes in GridView in Asp.Net