how to add a warning to DeleteCommand of gridview?

A

Averell

Hi,

I made a gridview with VWD. The gridview has the Delete button set
(ShowDeleteButton="True" in the <asp:CommandField>).
It works perfect, but i would like to add a warning before the record is
deleted to prevent deleting a wrong record.
I did this in the code-behind file:
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles
GridView1.RowDeleting
Dim jv As String
jv = "<script language='javascript'>" _
& " alert('beware!');" _
& "if (! confirm('if you want to delete it, click on OK'));" _
& " {window.location.href='mult.aspx'};" _
& "</script>"
Response.Write(jv)
End Sub

I see effectively the warning, but when i click on OK or on Cancel of the
Confirm, in both cases the record is deleted.
Is it possible to prevent that, and if yes, how?

Thanks for any hints
Averell
 
P

PeterKellner

Hi,

I made a gridview with VWD. The gridview has the Delete button set
(ShowDeleteButton="True" in the <asp:CommandField>).
It works perfect, but i would like to add a warning before the record is
deleted to prevent deleting a wrong record.
I did this in the code-behind file:
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles
GridView1.RowDeleting
Dim jv As String
jv = "<script language='javascript'>" _
& " alert('beware!');" _
& "if (! confirm('if you want to delete it, click on OK'));" _
& " {window.location.href='mult.aspx'};" _
& "</script>"
Response.Write(jv)
End Sub

I see effectively the warning, but when i click on OK or on Cancel of the
Confirm, in both cases the record is deleted.
Is it possible to prevent that, and if yes, how?

Thanks for any hints
Averell

You need to return the results of confirm.
Peter Kellner
http://peterkellner.net
 
B

Bob

Peter, i made a function and returned the confirm result, but even clicking
on Cancel, the record is deleted ...
I inserted an ALERT after the 'return false' and it's not executed. The Java
code stops indeed, but not the VB code deleting the row.

Is this the right way to proceed?
thanks


Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles
GridView1.RowDeleting
Dim jv As String
jv = "<script language='javascript'>" _
& "function check()" _
& "{" _
& " alert('let op');" _
& " var ok=confirm('if you want to delete it, click on OK');" _
& " if (!ok)" _
& " {" _
& " window.location.href='mult.aspx';" _
& " return false;" _
& " alert('this is not shown');" _
& "};" _
& "};" _
& "check();" _
& "</script>"
Response.Write(jv)
End Sub
 
O

olrt

You should look the example at :
ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/dv_aspnetcon/html/2c688b1a-93a4-4dad-b82b-63974bdbb13e.htm

Then you might paste the code of Page_Load into your Row_Deleting event
handler.
Don't forget to Cancel delete by default !!
 
N

NumbLock

ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/dv_aspnetcon/html/2
c688b1a-93a4-4dad-b82b-63974bdbb13e.htm

Check out this site:

http://www.codeproject.com/aspnet/NingLiangSimpleControl.asp

Ning Liang packaged up some javascript into a web control that will
return the results of it's confirm action in the request.form. You can
trap the delete button event, call the msgbox1.confirm function and trap
for the form variable in the load or prerender event since the
msgbox1.confirm does a postback.
 
O

olrt

With VWD 2005, I've the following message when I click to continue :

-----------------------------------------------------------------8<-------------------------------------------------------
Invalid postback or callback argument. Event validation is enabled
using <pages enableEventValidation="true"/> in configuration or <%@
Page EnableEventValidation="true" %> in a page. For security purposes,
this feature verifies that arguments to postback or callback events
originate from the server control that originally rendered them. If
the data is valid and expected, use the
ClientScriptManager.RegisterForEventValidation method in order to
register the postback or callback data for validation.
-----------------------------------------------------------------8<-------------------------------------------------------
 
C

CaffieneRush

What Peter Kellner means is that you need to inject some clientside
javascript when you're creating your delete button and return the
results of the Confirm.
In this scenario the RowDeleting serverside event would not even fire
if the user clicks Cancel because the delete click would be cancelled
on the clientside.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/GridViewEx10.asp
Do a search on the word 'OnClientClick' and you can see some specific
code on delete confirmation.

Regards,
Andy
 
A

Averell

Hi, i'm a little confused with all those solutions. Meanwhile i did this and
i feel i'm close to the solution (??).
The only thing i miss is how to pass the result of the function check() to
VB.
Thanks

Dim jv As String
jv = "function check()" _
& "{" _
& " alert('warning');" _
& " var ok=confirm(if you want to delete; click on OK');" _
& " if (!ok)" _
& " {" _
& " window.location.href='mult.aspx';" _
& " return false;" _
& "};" _
& "};" _
& "check();"

Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "myscript", jv,
True)

dim x as string
x= result of function check() : HOW TO DO THIS?
if x="False" then e.cancel=True
 
C

CaffieneRush

I finally see where the confusion comes from.
The return we are talking about is not a return to the serverside code
but to the button's click event. So cancel will cancel the click event
while a confirm will continue with the click which would then go onto
trigger a postback to the server.

Take a look at Edwin's code in this thread to see a complete example of
this in action.
My only suggestion is that the line below can be simplified within
Default2.aspx.vb:
If b IsNot Nothing Then b.Attributes.Add("onclick",
"javascript:window.event.returnValue=confirm('Delete?');")

to:
If b IsNot Nothing Then b.OnClientClick = "return confirm('Delete?');"

Regards,
Andy

ps. If you are still resistant to this simple solution then you can use
javascript to write the value of your ok variable to a hidden field in
the page and check this hidden field on the postback.
 
E

Edwin Knoppert

I never understood the differences myself.
For my code i was looking for a simple click-abort (on no, abort), that's
all..
However afaik at that time the return statement itself was insufficient
somehow and still produced a round-trip??

I forgot what exactly happend at that time, maybe someone can shed some
light on these differences?

Thanks,
 
E

Edwin Knoppert

Btw, imo by removing "javascript:" from the line you assume that the browser
interpret's the code as javascript by default.
Maybe you better keep "javascript:" in at all times.
I have not investigated this but i assumed this.
 
C

CaffieneRush

Fair point about assuming javascript. It is better to be safe so code
below has be changed.
If b IsNot Nothing Then b.OnClientClick = "javascript:return
confirm('Delete?');"

I don't understand how a postback could have occured after returning
false to the control's OnClick event.
The line below should cause the button to never postback to the server
no matter how many time it is clicked.
If b IsNot Nothing Then b.OnClientClick = "javascript:return false;"

Regards,
Andy
 
E

Edwin Knoppert

It was a long time ago and i had much to learn about js and asp.net.
I really don't know what happened and at this time i have no time to check
this out unf..
I just polled if one had a similar experiance :)

Thanks,
 

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

Top