Solved

DataGrid - Delete Confirmation

Posted on 2010-09-24
6
666 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

752 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