Click here to Skip to main content
11,640,445 members (61,905 online)
Click here to Skip to main content

ASP.NET GridView delete confirmation using asp:CommandField

, 22 Jan 2009 CPOL 123.4K 25
Rate this:
Please Sign up or sign in to vote.
ASP.NET GridView delete confirmation using asp:CommandField with LINQ to SQL.

Introduction

There are a few articles out there that already that deals with how to get a JavaScript delete confirmation up in a GridView, I know..., but none of them were exactly what I was after. My method can be used when Updates and Deletes are automatically handled by an asp:LinqDataSource control. You will need your own LINQ DataContext class for your database, and I guess the same principle can be applied to other data source classes too.

Implementation

First, you will need a GridView control that gets its data from a LinqDataSource. Make sure you specify the OnRowDataBound attribute and use the LinqDataSource with delete enabled.

<asp:GridView ID="GridView1" runat="server" PageSize="10" AllowPaging="True" 
    AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="RecordID" 
    DataSourceID="MyDataSource" OnRowDataBound="GridView_RowDataBound">
    <Columns>
        <asp:BoundField DataField="Code" HeaderText="Code" SortExpression="Code" 
                ItemStyle-Width="100px" />
        <asp:BoundField DataField="Description" HeaderText="Description" 
                SortExpression="Description" ItemStyle-Width="560px" 
                ControlStyle-Width="560px" />
        <asp:CommandField HeaderImageUrl="..\Images\DeleteImg.png" ShowDeleteButton="True" 
                DeleteImageUrl="..\Images\DeleteImg.png" DeleteText="Delete Record" 
                ItemStyle-Font-Size="8pt" ItemStyle-Width="30px" ButtonType="Image">
        </asp:CommandField>
    </Columns>
</asp:GridView>
<asp:LinqDataSource ID="MyDataSource" runat="server" 
    ContextTypeName="MyNameSpace.MyDbDataContext" 
    EnableUpdate="true" EnableDelete="true" TableName="MyTable">
</asp:LinqDataSource>

Except for the OnRowDataBound, this control should now work with delete functionality without writing a single line of code.

The next step is to implement the GridView_RowDataBound function which looks like this:

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // loop all data rows
        foreach (DataControlFieldCell cell in e.Row.Cells)
        {
           // check all cells in one row
           foreach (Control control in cell.Controls)
           {
                // Must use LinkButton here instead of ImageButton
                // if you are having Links (not images) as the command button.
                ImageButton button = control as ImageButton;
                if (button != null && button.CommandName == "Delete")
                    // Add delete confirmation
                    button.OnClientClick = "if (!confirm('Are you sure " + 
                           "you want to delete this record?')) return;";
            }
        }
    }
}

The important bit here is to use exactly the OnClientClick content provided (except the wording in the message, of course). If you, for example, instead, had "return confirm('Are you sure you want to delete the record')" like many others have suggested, this would mean the Delete command would never be posted back to the server. The actual onclick function call generated/rendered by ASP.NET - if you follow the code supplied above - will be something like this...

onclick="if (!confirm('Are you sure you want to delete this record?')) return;
javascript:__doPostBack('ctl00$cphMain$GridView1','Delete$9')"

You can see that the JavaScript you provide is appended with the postback call from ASP.NET. If you return in your JavaScript prematurely, the postback will never be triggered.

Note: I used images for my command buttons in my GridView. Remember to cast to the LinkButton class instead of the ImageButton class if you use text-links for command buttons.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

elSeten
Australia Australia
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 4 Pin
Alberttogar2428-Aug-13 7:43
memberAlberttogar2428-Aug-13 7:43 
QuestionGreat job Pin
ashumeerut9-Aug-13 0:13
professionalashumeerut9-Aug-13 0:13 
GeneralMy vote of 5 Pin
CycloneBoy2-Nov-12 18:51
memberCycloneBoy2-Nov-12 18:51 
GeneralMy vote of 5 Pin
Armando de la Torre13-Oct-11 19:14
memberArmando de la Torre13-Oct-11 19:14 
QuestionYou need to some change for make it useful. See it.... Pin
Md. Touhidul Alam Shuvo2-Oct-11 20:51
memberMd. Touhidul Alam Shuvo2-Oct-11 20:51 
GeneralThanks Pin
Roy Oliver22-Nov-10 9:10
memberRoy Oliver22-Nov-10 9:10 
Generalmonth name displayed in hindi Pin
jaya_k_t29-Sep-09 23:13
memberjaya_k_t29-Sep-09 23:13 
Generalit is working perfect with sqldatasource with slight change Pin
Prabakaran_0822-Jul-09 22:20
memberPrabakaran_0822-Jul-09 22:20 
Generalcancel is not working on confirmation Pin
jignesh prajapati6-Apr-09 23:38
memberjignesh prajapati6-Apr-09 23:38 
GeneralRe: cancel is not working on confirmation Pin
Member 282773129-Apr-09 3:29
memberMember 282773129-Apr-09 3:29 
GeneralRe: cancel is not working on confirmation Pin
Member 964133513-Nov-14 1:15
memberMember 964133513-Nov-14 1:15 
GeneralNice Pin
Peter Mills20-Feb-09 6:37
memberPeter Mills20-Feb-09 6:37 
Generalsomething is wrong Pin
carlos david12-Feb-09 2:23
membercarlos david12-Feb-09 2:23 
GeneralRe: something is wrong Pin
Lasse Seten14-Feb-09 21:57
memberLasse Seten14-Feb-09 21:57 
GeneralRe: something is wrong Pin
carlos david16-Feb-09 19:09
membercarlos david16-Feb-09 19:09 
GeneralRe: something is wrong Pin
carlos david16-Feb-09 19:23
membercarlos david16-Feb-09 19:23 
GeneralLittle Optimization Pin
salvorapi22-Jan-09 23:00
membersalvorapi22-Jan-09 23:00 
GeneralRe: Little Optimization Pin
-Dr_X-27-Jan-09 6:22
member-Dr_X-27-Jan-09 6:22 
QuestionWill it work for SqlDataSource ? Pin
Member 447707522-Jan-09 17:59
memberMember 447707522-Jan-09 17:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 22 Jan 2009
Article Copyright 2009 by elSeten
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid