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

J

JenHu

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
 
K

Karl Seguin

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 said:
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
 
D

David

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
 
K

Karl Seguin

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
 
K

Ken Cox [Microsoft MVP]

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 said:
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
 
D

David Barkol

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.
 
G

Guest

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top