Solved

DataGrid - Delete Confirmation

Posted on 2010-09-24
6
650 Views
Last Modified: 2013-11-26
I have been trying to find a solution to getting a confirmation dialogue box to appear when deleting a record from a DataGrid on an ASP.NET 2.0 web page.  I have found some articles that show methods of how to do this by adding a template column to the DataGrid but these all show how to do this when buolding the DataGrid in the aspx file and not in the code behind page as I am doing.

Also, I do not want to fire the built in delete command as I want to take custom actions when this button is clicked.

The code in the aspx file is simply:

                    <asp:GridView ID="gvOrgEntries" runat="server" Width="100%" AutoGenerateColumns="False" AllowSorting="True" >
                        <HeaderStyle HorizontalAlign="Left" />
                    </asp:GridView>

And then in the code behind I have this code to build the datagrid display:

               bf = New BoundField
                bf.DataField = "PerformanceLocation"
                bf.HeaderText = "Location"
                bf.SortExpression = "PerformanceLocation"
                Me.gvOrgEntries.Columns.Add(bf)

                bf = New BoundField
                bf.DataField = "PerformanceDate"
                bf.HeaderText = "Date"
                bf.SortExpression = "PerformanceDate"
                Me.gvOrgEntries.Columns.Add(bf)

                Dim sb As New CommandField()
                sb.SelectText = "Delete"
                sb.ShowSelectButton = True
                sb.ButtonType = ButtonType.Button
                Me.gvOrgEntries.Columns.Add(sb)

Does anyone know how to add code that will pop up a confirmation dialoge box in this scenario?
0
Comment
Question by:BorisMatthews
6 Comments
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 33757937
Here you have the whole explanation. Check  the link that I give it to you

http://www.dotnetjohn.com/articles.aspx?articleid=68
0
 

Author Comment

by:BorisMatthews
ID: 33759526
Yes, I have seen this article but this explains how to do this when building the layout of the DataGrid in the aspx file using a template column but as you can see from my code I have a preference for building the columns and layout of the DataGrid in the Code Behind file and I cannot see how to translate the code in the suggested article to use this when I do not add the DaatGrid components in the aspx file.
0
 
LVL 3

Expert Comment

by:revaluser
ID: 33760752
Delete Button Template in the Gridview



<asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="btnDelete" runat="server" Text="Delete" OnCommand="btnDelete_Command" OnCommand="btnDelete_Command" CommandArgument='<%#Eval("ColoumVauewhcichisrequires") %>' />
                    </ItemTemplate>
                </asp:TemplateField>

Call this function from the gridview

OnRowDataBound="gridview_RowBound"



protected void gridview_RowBound(object sender, GridViewRowEventArgs e)
    {
        Button btnDelete = (Button)e.Row.FindControl("btn");
        if (btnDelete != null)
        {
            btnDelete.OnClientClick = "javascript:return confirm('are you sure!')";
        }
    }

Then Have the Custom Command

protected void btnDelete_Command(object Sender, CommandEventArgs e)
    {
        //e.CommandArgument;
        //Use e.CommandArgument and write the Custom Delete functionality
    }

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:BorisMatthews
ID: 33761209
This is basically the same answer as k-designers suggested.  What I am trying to do is to do this totally from within the VB code behind file and without having to do anything other than the existing code in the aspx file which just inserts the basic reference to the datagrid control:

<asp:GridView ID="gvOrgEntries" runat="server" Width="100%" AutoGenerateColumns="False" AllowSorting="True" >
                        <HeaderStyle HorizontalAlign="Left" />
                    </asp:GridView>
0
 
LVL 12

Expert Comment

by:Kaushal Arora
ID: 33761589
This is the sample you can do this in the code. The sample is in C#, you can read it and try to do it using TemplateColumn. Have a look at this link:

http://www.codeproject.com/KB/webforms/ASPNET_DataGrid_creation.aspx

Hope it helps you.
0
 
LVL 12

Accepted Solution

by:
Kaushal Arora earned 500 total points
ID: 33761591
While adding the Button control in place DDL as given in the sample, you can assign the OnClientClick event of the Button which will help you achieve what you want.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question