Home All Groups Group Topic Archive Search About

Are you sure? for delete in GridView

Author
16 Mar 2006 9:56 PM
David Thielen
Hi;

When a user clicks on the delete link for a row in a GridView, how can I
have it popup an alert asking them if they are sure?

thanks - dave

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Author
17 Mar 2006 2:53 AM
Steven Cheng[MSFT]
Hi Dave,

Thanks for posting.

As for the displaying a confirm message when the user click "delete" button
on a gridview, I think it can be done by registering some client script for
the delete button's "onclick" client-side event. However, we need to get
the reference of the delete button (in RowCreated event) so as to register
the script for it. To make the work easy, we can consider convert the
CommandField to TemplateField so that the field is template based and we
can specify the "id" for the delete button. e.g:

Here is a simple gridview I used for test:

==================
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="CategoryID"
            DataSourceID="SqlDataSource2"
OnRowDeleting="GridView1_RowDeleting" OnRowCreated="GridView1_RowCreated1">
            <Columns>
                <asp:TemplateField ShowHeader="False">
                    <EditItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server"
CausesValidation="True" CommandName="Update"
                            Text="Update"></asp:LinkButton>
                        <asp:LinkButton ID="LinkButton2" runat="server"
CausesValidation="False" CommandName="Cancel"
                            Text="Cancel"></asp:LinkButton>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server"
CausesValidation="False" CommandName="Edit"
                            Text="Edit"></asp:LinkButton>
                        <asp:LinkButton ID="LinkButton2" runat="server"
CausesValidation="False" CommandName="Select"
                            Text="Select"></asp:LinkButton>
                        <asp:LinkButton ID="LinkButton3" runat="server"
CausesValidation="False" CommandName="Delete"
                            Text="Delete"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="CategoryID"
HeaderText="CategoryID" ReadOnly="True" SortExpression="CategoryID"  />
                <asp:BoundField DataField="CategoryName"
HeaderText="CategoryName" SortExpression="CategoryName" />
                <asp:BoundField DataField="Description"
HeaderText="Description" SortExpression="Description" />
            </Columns>
        </asp:GridView>
===============================================

================
protected void GridView1_RowCreated1(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton btn = e.Row.FindControl("LinkButton3") as LinkButton;

            if (btn != null)
            {
                btn.Attributes["onclick"] = "if(confirm('Are you sure to
delte this record?')==false){return false;}";
            }
        }
    }
=================

You can see that we can just locate the delete button through its id in
GridView's RowCreated event and attach the "onclick" script.

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Author
17 Mar 2006 3:19 AM
David Thielen
Hi;

What about http://www.codeproject.com/aspnet/GridViewRedux.asp - search for
"put delete confirmations in" - what is this doing???

or
http://www.xmlpitstop.com/ArticleManagement/DisplayArticle.aspx?ResourceID=101 - search for "Are you sure" - any idea how this works?

The above look simpler but I worry they are not something that will work
everywhere.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com
Author
17 Mar 2006 6:39 AM
Steven Cheng[MSFT]
Just had a quick look on them, I think the theory should be the same, use
the gridview's certain event to reference the button instance and register
script code. Also, this is a common approach ever since the ASP.NET 1.1
datagrid control...

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)