Solved

DataGrid - Delete Confirmation

Posted on 2010-09-24
6
647 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:BorisMatthews
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now