In ASP.Net web pages, Grid View Delete row functionality is a very common feature of modern websites, here is the script for the confirmation to delete record without post back using “Page.ClientScript.RegisterStartupScript” feature.
- Bind grid on page load with Check() function.
- Call Check() function whenever you binding data to grid.
- Where “grdName” name of grid and “chkSelect” name of row check box
protected void Check()
{
{
string checkid = “”;
checkid += “function val(id){“;
checkid += “var flg=0;”;
checkid += “function val(id){“;
checkid += “var flg=0;”;
foreach (GridViewRow grv in grdName.Rows)
{
CheckBox chk1;
chk1 = (CheckBox)grv.FindControl(“chkSelect”);
checkid += “if(document.getElementById(‘” + chk1.ClientID + “‘).checked==true){“;
checkid += “flg=1;}”;
}
checkid += “if(flg!=1){“;
checkid += “alert(‘Select Atleast One Row(s)’);return false;}”;
checkid += “if(flg==1 && id==1){“;
checkid += “if(!confirm(‘Do You Want To Delete Selected Rows(s) ?’)){return false;}}”;
checkid = checkid + “}”;
Page.ClientScript.RegisterClientScriptBlock(GetType(), “myscript2″, checkid, true);
btnDelete.Attributes.Add(“onclick”, “return val(1);”);
}
<asp:GridView ID=”grdName” runat=”server” AutoGenerateColumns=”False”>
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID=”chkall” runat=”server” />
<HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID=”chkSelect” runat=”server” />
<ItemTemplate>
<asp:TemplateField>
<Columns>
<asp:GridView>
Advertisement