By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,550 Members | 1,733 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,550 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

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" <je**********@hotmail-dot-com.no-spam.invalid> wrote in message
news:42********@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

Nov 19 '05 #2

P: n/a
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

Nov 19 '05 #3

P: n/a
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" <da**********@neudesic.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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

Nov 19 '05 #4

P: n/a
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" <je**********@hotmail-dot-com.no-spam.invalid> wrote in message
news:42********@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


Nov 19 '05 #5

P: n/a
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.

Nov 19 '05 #6

P: n/a
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.

Nov 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.