Delete Confirmation in ASP.NET GridView

While using GridView in ASP.NET, you will not be able to write OnClientClick code for asp:CommandField Link / Button directly. However, if you want to generate Confirmation for Delete Link / Button, you have to write Client Confirmation code from back end at RowDataBound Event of GridView. Have a look at this code snippet.


protected void trialGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{   
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        try
        {
            // reference the Delete LinkButton
            LinkButton deleteButton = (LinkButton)e.Row.Cells[1].Controls[0];
            deleteButton.OnClientClick = "return confirm('Are you sure, you want to delete this Record ?');";
        }
        catch(Exception exception)
        {
            Response.Write(exception.ToString());
        }
    }
}

Thanks
A Rahim Khan

Advertisements
  1. I do comment on blogs – not to leave a digital footprint – which of course is good, but because I really appreciate all the time and effort someone goes through to post useful information. Which by the way, you offer great insight into this. Thanks!

  2. sorry sir
    can u tell me how to make a login form with visual studio 2010 and with language c#

    make a simple syntax , thx :)

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: