Solved

DataGrid - Delete Confirmation

Posted on 2010-09-24
6
669 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

624 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