Solved

DataGrid - Delete Confirmation

Posted on 2010-09-24
6
662 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Generate Unique ID in VB.NET 21 100
disable data migrations in visual studio 2017 4 42
Record locking on classic ASP 3 37
c#, case, if 4 16
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

735 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