Thursday, December 4, 2008

To get confirm using javascript while deleting a Row in GridView in C#, asp.net

Hi,
I want to get confirmation from the user while deleting particular record from the GridView.
In code-behind, we can do this by inserting the javascript.This can be written in the RowDataBound Event(OnRowDataBound=
sampleStudGridView_RowDataBound) of the GridView.

In Code-Behind,
protected void sampleStudGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
LinkButton deleteLinkButton = (LinkButton)e.Row.FindControl("deleteLinkButton");
if (deleteLinkButton != null)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
deleteLinkButton.Attributes.Add("onClick","javascript:return "+ "confirm('Are you sure you want to delete this record ? "+DataBinder.Eval(e.Row.DataItem,"sname")+"')");
}
}
}

No comments: