?
Solved

How to Provide a Delete Confirmation in GridView Control When Delete is Clicked.

Posted on 2006-11-20
2
Medium Priority
?
205 Views
Last Modified: 2008-02-01
Greetings;

In Visual Studio 2003 VB.NET for ASP.NET when I wanted to associate a JavaScript alert when a user clicked the delete button (or delete link) to confirm the user really wanted to delete the row in a datagrid control I used the following example in the ItemDataBound event in the codebehind file:

----------------------------------------- BEGIN

      Private Sub dlContact_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlContact.ItemDataBound
            Dim btnDelete As Button

            If e.Item.ItemType = ListItemType.Header Or _
             e.Item.ItemType = ListItemType.Footer Then
                  Exit Sub
            End If

            btnDelete = CType(e.Item.FindControl("btnDelete"), Button)
            If btnDelete Is Nothing Then
                  Exit Sub
            End If
            btnDelete.Attributes("onclick") = "javascript:return confirm('Are you sure you want to delete this contact?');"
      End Sub

----------------------------------------- END

In Visual Studio 2005 VB.NET for ASP.NET when I use the <asp:CommandField> tag to create Edit, Select, Delete buttons (or links) what is the equivalent method of providing a confirmation when the user clicks the delete button?


Much thanks ... David
0
Comment
Question by:David Bach
[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
2 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 17981873
It should still be a matter of adding the "OnClick" attribute to the button by the buttons ID in the code-behind after the data is bound.
0
 
LVL 9

Accepted Solution

by:
kraffay earned 2000 total points
ID: 17981958
I would highly recommend converting your CommandFields to TemplateFields.  CommandFields cause redundant post back and greatly slow down gridview performance.  

Here I have a delete button:

 <asp:TemplateField>
                                    <HeaderTemplate>
                                        Delete
                                    </HeaderTemplate>
                                    <ItemStyle HorizontalAlign="Left" VerticalAlign="Bottom" />
                                    <ItemTemplate>
                                        <asp:ImageButton ImageAlign="AbsBottom" Height="15px" ImageUrl="images/icon-delete.gif"
                                            ID="btnDelete" runat="server" CommandArgument="<%# Bind('UserName')%>" CausesValidation="False" />
                                    </ItemTemplate>
                                </asp:TemplateField>

At run time, I attached the alert to the delete button's click event:

    protected void grdUsers_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton btnDelete = (ImageButton)e.Row.FindControl("btnDelete");
            btnDelete.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this user?');");
        }
    }
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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month12 days, 16 hours left to enroll

777 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