How to use confirmation box on delete a row in a datagrid?

Discussion in 'ASP .Net' started by JenHu, Feb 2, 2005.

  1. JenHu

    JenHu Guest

    Hi experts,

    I want to add a delete button in my datagrid, and before it deletes
    from database, I want to have a confirm dialog box for this deletion.
    I am entry level to the vb.net, and don't know java or c#.

    The problem is, when I tested the program, there is no pop-up
    confirmation box for the deletion, can you show me what' wrong with
    this code? Thank you.

    <asp:datagrid id="dgAppStatus" Runat="server"
    AutoGenerateColumns="False" OnDeleteCommand="dgAppStatus_Delete"
    DataKeyField="EmployID">

    <Columns>
    <asp:BoundColumn DataField="EmployID" SortExpression="EmployID"
    HeaderText="Employee ID"></asp:BoundColumn>
    <asp:BoundColumn DataField="LASTNAME" SortExpression="LASTNAME"
    HeaderText="Last Name"></asp:BoundColumn>
    <asp:BoundColumn DataField="FRSTNAME" SortExpression="FRSTNAME"
    HeaderText="First Name"></asp:BoundColumn>
    <asp:TemplateColumn><ItemTemplate>
    <asp:Button ID="btnDelete" Text="Delete"
    Runat="server"></asp:Button></ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:datagrid>

    Sub dgAppStatus_Delete(ByVal sender As Object, ByVal e As
    DataGridCommandEventArgs)
    btnDelete.Attributes.Add("onclick", "return confirm ('Are you
    sure you wish to delete this record?')")
    End Sub
    *---------------------------------*
    Posted at: http://www.GroupSrv.com
    *---------------------------------*

    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    http://www.usenet.com
     
    JenHu, Feb 2, 2005
    #1
    1. Advertising

  2. JenHu

    Karl Seguin Guest

    The problem with your solution is that you are adding the client-side
    onClick after the button has already been clicked and the processing moved
    to the server.

    I'm aware of two solutions: Create your own ConfirmDelete button (my
    prefered method), or hook into the OnItemDataBound event of the dgAppStatus
    to do the btnDelete.Attributes.Add("...", ...) there
    (http://openmymind.net/databinding/index.html#4.2)

    If you do onClick in the html, it'll associate it with the server-side
    meaning...doh! ASP.Net has a new attribute called onClientSideClick or
    something which avoids the problem..

    the confirmDelete control would look something like:

    public class ConfirmDelete
    inherits Button

    private _message as string
    public property Message() as string
    get
    return _message
    end get
    set
    _message = value
    end set
    end property

    Protected Overrides Sub Render(ByVal writer As Writer)
    if not _message is nothing
    Attributes.Add("OnClick", _message)
    end if
    myBase.Render(writer)
    End Sub
    end class


    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "JenHu" <-spam.invalid> wrote in message
    news:4201310d_3@127.0.0.1...
    > Hi experts,
    >
    > I want to add a delete button in my datagrid, and before it deletes
    > from database, I want to have a confirm dialog box for this deletion.
    > I am entry level to the vb.net, and don't know java or c#.
    >
    > The problem is, when I tested the program, there is no pop-up
    > confirmation box for the deletion, can you show me what' wrong with
    > this code? Thank you.
    >
    > <asp:datagrid id="dgAppStatus" Runat="server"
    > AutoGenerateColumns="False" OnDeleteCommand="dgAppStatus_Delete"
    > DataKeyField="EmployID">
    >
    > <Columns>
    > <asp:BoundColumn DataField="EmployID" SortExpression="EmployID"
    > HeaderText="Employee ID"></asp:BoundColumn>
    > <asp:BoundColumn DataField="LASTNAME" SortExpression="LASTNAME"
    > HeaderText="Last Name"></asp:BoundColumn>
    > <asp:BoundColumn DataField="FRSTNAME" SortExpression="FRSTNAME"
    > HeaderText="First Name"></asp:BoundColumn>
    > <asp:TemplateColumn><ItemTemplate>
    > <asp:Button ID="btnDelete" Text="Delete"
    > Runat="server"></asp:Button></ItemTemplate>
    > </asp:TemplateColumn>
    > </Columns>
    > </asp:datagrid>
    >
    > Sub dgAppStatus_Delete(ByVal sender As Object, ByVal e As
    > DataGridCommandEventArgs)
    > btnDelete.Attributes.Add("onclick", "return confirm ('Are you
    > sure you wish to delete this record?')")
    > End Sub
    > *---------------------------------*
    > Posted at: http://www.GroupSrv.com
    > *---------------------------------*
    >
    > Posted Via Usenet.com Premium Usenet Newsgroup Services
    > ----------------------------------------------------------
    > ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    > ----------------------------------------------------------
    > http://www.usenet.com
     
    Karl Seguin, Feb 2, 2005
    #2
    1. Advertising

  3. JenHu

    David Guest

    The column for your delete button should look like this:

    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:Button ID="btnDelete" Text="Delete"
    onclick="return confirm('Are you sure you want to delete')"
    onserverclick="dgAppStatus_Delete"
    runat="server">
    </asp:Button>
    </ItemTemplate>
    </asp:TemplateColumn>

    David Barkol
    www.neudesic.com
     
    David, Feb 2, 2005
    #3
  4. JenHu

    Karl Seguin Guest

    David,
    correct me if I'm wrong but there is no such thing as a OnServerClick...and
    the onClick will get rendered as the server-side click?

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "David" <> wrote in message
    news:...
    > The column for your delete button should look like this:
    >
    > <asp:TemplateColumn>
    > <ItemTemplate>
    > <asp:Button ID="btnDelete" Text="Delete"
    > onclick="return confirm('Are you sure you want to delete')"
    > onserverclick="dgAppStatus_Delete"
    > runat="server">
    > </asp:Button>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    >
    > David Barkol
    > www.neudesic.com
    >
     
    Karl Seguin, Feb 2, 2005
    #4
  5. Hi Jennifer,

    Here's how I'd handle that code. Let us know if the sample below helps?

    Ken
    Microsoft MVP [ASP.NET]
    Toronto

    Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    If Not IsPostBack Then
    dgAppStatus.DataSource = CreateDataSource()
    dgAppStatus.DataBind()
    End If
    End Sub

    Function CreateDataSource() As DataTable
    Dim dt As New DataTable
    Dim dr As DataRow
    dt.Columns.Add(New DataColumn _
    ("EmployID", GetType(Int32)))
    dt.Columns.Add(New DataColumn _
    ("LASTNAME", GetType(String)))
    dt.Columns.Add(New DataColumn _
    ("FRSTNAME", GetType(String)))

    Dim i As Integer
    For i = 0 To 1
    dr = dt.NewRow()
    dr(0) = i
    dr(1) = "FirstName " + i.ToString()
    dr(2) = "LastName " + i.ToString()
    dt.Rows.Add(dr)
    Next i
    Return dt
    End Function 'CreateDataSource

    Private Sub dgAppStatus_ItemCommand _
    (ByVal source As Object, ByVal e _
    As System.Web.UI.WebControls.DataGridCommandEventArgs) _
    Handles dgAppStatus.ItemCommand
    If e.CommandName = "DeleteIt" Then
    ' Call delete routine here and rebind to the dataset
    Response.Write("Would have deleted row index: " & _
    e.Item.ItemIndex.ToString)
    End If
    End Sub

    Private Sub dgAppStatus_ItemCreated(ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
    Handles dgAppStatus.ItemCreated
    Dim btnDel As Button
    Dim drow As DataGridItem
    drow = e.Item
    btnDel = drow.FindControl("btnDelete")
    If Not IsNothing(btnDel) Then
    btnDel.Attributes.Add("onclick", _
    "return confirm ('Are you sure you wish to delete this
    record?')")
    End If
    End Sub

    <asp:datagrid id="dgAppStatus" Runat="server"
    AutoGenerateColumns="False" DataKeyField="EmployID">
    <columns>
    <asp:boundcolumn DataField="EmployID"
    SortExpression="EmployID" HeaderText="Employee ID"></asp:boundcolumn>
    <asp:boundcolumn DataField="LASTNAME"
    SortExpression="LASTNAME" HeaderText="Last Name"></asp:boundcolumn>
    <asp:boundcolumn DataField="FRSTNAME"
    SortExpression="FRSTNAME" HeaderText="First Name"></asp:boundcolumn>
    <asp:templatecolumn>
    <itemtemplate>
    <asp:button ID="btnDelete"
    CommandName="DeleteIt" Text="Delete" Runat="server"></asp:button>
    </itemtemplate>
    </asp:templatecolumn>
    </columns>
    </asp:datagrid>



    "JenHu" <-spam.invalid> wrote in message
    news:4201310d_3@127.0.0.1...
    > Hi experts,
    >
    > I want to add a delete button in my datagrid, and before it deletes
    > from database, I want to have a confirm dialog box for this deletion.
    > I am entry level to the vb.net, and don't know java or c#.
    >
    > The problem is, when I tested the program, there is no pop-up
    > confirmation box for the deletion, can you show me what' wrong with
    > this code? Thank you.
    >
    > <asp:datagrid id="dgAppStatus" Runat="server"
    > AutoGenerateColumns="False" OnDeleteCommand="dgAppStatus_Delete"
    > DataKeyField="EmployID">
    >
    > <Columns>
    > <asp:BoundColumn DataField="EmployID" SortExpression="EmployID"
    > HeaderText="Employee ID"></asp:BoundColumn>
    > <asp:BoundColumn DataField="LASTNAME" SortExpression="LASTNAME"
    > HeaderText="Last Name"></asp:BoundColumn>
    > <asp:BoundColumn DataField="FRSTNAME" SortExpression="FRSTNAME"
    > HeaderText="First Name"></asp:BoundColumn>
    > <asp:TemplateColumn><ItemTemplate>
    > <asp:Button ID="btnDelete" Text="Delete"
    > Runat="server"></asp:Button></ItemTemplate>
    > </asp:TemplateColumn>
    > </Columns>
    > </asp:datagrid>
    >
    > Sub dgAppStatus_Delete(ByVal sender As Object, ByVal e As
    > DataGridCommandEventArgs)
    > btnDelete.Attributes.Add("onclick", "return confirm ('Are you
    > sure you wish to delete this record?')")
    > End Sub
    > *---------------------------------*
    > Posted at: http://www.GroupSrv.com
    > *---------------------------------*
    >
    > Posted Via Usenet.com Premium Usenet Newsgroup Services
    > ----------------------------------------------------------
    > ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    > ----------------------------------------------------------
    > http://www.usenet.com
     
    Ken Cox [Microsoft MVP], Feb 2, 2005
    #5
  6. JenHu

    David Barkol Guest

    Actually you are correct; for an asp button control. If it were a
    simple link instead of a button then you could use the 'onserverclick'
    event:

    <a id="linkDelete"
    onclick="return confirm('Are you sure you want to delete')"
    onserverclick="DeleteMethod" runat="server">
    Delete
    </a>

    For a button control your solution works perfectly. I prefer using the
    link, sorry for the mix up.
     
    David Barkol, Feb 2, 2005
    #6
  7. Thanks for that,

    How do we identify which row we want to delete on the server side?

    I was looking at the asp:Hyperlink component for a similar thing.

    thanks,

    Paul

    "David Barkol" wrote:

    > Actually you are correct; for an asp button control. If it were a
    > simple link instead of a button then you could use the 'onserverclick'
    > event:
    >
    > <a id="linkDelete"
    > onclick="return confirm('Are you sure you want to delete')"
    > onserverclick="DeleteMethod" runat="server">
    > Delete
    > </a>
    >
    > For a button control your solution works perfectly. I prefer using the
    > link, sorry for the mix up.
    >
    >
     
    =?Utf-8?B?UGF1bCBK?=, Feb 3, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sramruttun

    Re: delete confirmation in datagrid

    sramruttun, Jul 4, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    611
    sramruttun
    Jul 4, 2003
  2. Nedu N
    Replies:
    2
    Views:
    387
    Robert McLaws
    Dec 4, 2003
  3. bienwell
    Replies:
    5
    Views:
    5,208
    bienwell
    Aug 13, 2006
  4. Grant

    Delete confirmation box?

    Grant, Jan 5, 2005, in forum: ASP .Net Web Controls
    Replies:
    5
    Views:
    311
    Grant
    Jan 7, 2005
  5. ASMJ
    Replies:
    0
    Views:
    202
Loading...

Share This Page