Delete Confirmation on a ASP.NET GridView

So I want something a little more than just the Default Delete option that you get with the GridView control. When you click this Delete button, your record just disappears and you’re left for a few moments thinking to yourself, “did I really want to delete that?”.  Well I thought I needed to find a method of being able to prompt the user for a confirmation before actually deleting the record.

After searching the web I came across a version that worked really well and was simple to implement, so I thought I would share it here.

      <asp:GridView
        ID="GridView1"
        DataSourceID="DataSource1"
        DataKeyNames="ID"
        AutoGenerateColumns="false"
        runat="server">
        <Columns>
        <asp:BoundField
          HeaderText="Record ID"
          DataField="ID"
          ReadOnly="true" />
        <asp:BoundField
          HeaderText="Movie Title"
          DataField="MOVIE" />
        <asp:CommandField
          HeaderText="Edit?"
          ButtonType="Button"
          ShowSelectButton="true"
          SelectText="Select" />
        <asp:TemplateField>
          <HeaderTemplate>
            Delete?
          </HeaderTemplate>
          <ItemTemplate>
            <asp:Button
              ID="btnDelete"
              OnClientClick="return confirm('Are you sure you want to delete this record?');"
              CommandName="Delete"
              Text="Delete"
              runat="server" />
          </ItemTemplate>
        </asp:TemplateField>
        </Columns>
      </asp:GridView>

Technorati Tags: ,,
Advertisements

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: