Confirmation message upon deleting an item from GridView control in ASP.NET

Confirmation message upon deleting an item from GridView control in ASP.NET

Lots of users click the delete button within the GridView with no attention then the result is that the item will be deleted from the GridView, so we can add confirmation message to appear to tell the user “Are you sure you want to delete this item?”.

First of all we have to attach the javascript code for each delete button within the GridView, we can do that in the itemdatabound event of the GridView:

HttpContext.Current.Response.Write(MyString);
LinkButton deleteButton = e.Item.Cells[0].Controls[0];
deleteButton.Attributes.Add("onclick","javascript:return confirm('Are you sure you want to delete this data?')");

Where the first cell in the GridView is a linkbutton. So when the user clicks the delete button then a confirmation message appears so if the user clicks No then nothing happens else postback will happen then it should be a server event such as a onDeleteCommand that fires when the user clicks the delete button as

private void dgPendingCompleteForms_Delete(Object sender, DataGridCommandEventArgs e)
{
    //Put your code here when the user presses the yes button on the confirmation meessagebox
}

Share this post

Comments (3)

  • Anonymous Reply

    its great, but I believe there’s a javascript (Ajax) confirmation can do the same functionality

    November 9, 2007 at 1:42 PM
  • Anonymous Reply

    You can also put the confirmation directly in the linkbutton onclientclick property instead of looking for it in the ItemDataBound event and setting the onclick attribute OnClientClick=”return confirm(‘Are you sure you want to delete this item?’);” Of course the linkbutton will be in a template column.

    November 10, 2007 at 1:45 PM
  • Saiful Alam Reply

    Your blog is very nice…
    visit my blog asp.net example

    December 14, 2007 at 1:48 PM

Leave a Reply to Saiful Alam Cancel reply

Your email address will not be published. Required fields are marked *