Elegant Way to Handle "Are you Sure" Dialog on Client

G

Guest

Let's say I have a web forms button which performs a Delete or something. I'd
like a click on the button to pop an "Are you Sure" dialog on the client side
and then only execute the button's OnClick operation on the server side if
the user had clicked Ok (as opposed to Cancel) on the pop-up dialog.

I know I can do this all myself with JavaScript, but I was just wondering if
any such functionality is already built-in in ASP.NET 2.0 so that the Button
control actually renders the necessary client-side scripting by itself.

Alex
 
T

Teemu Keiski

Hi,

using OnClientClick attribute of Button

OnClientClick="if(!confirm('Are you sure?'))return false;"
 
G

Guest

Oooh! I didn't know about this OnClientClick thing. That's pretty cool. But
I'm not sure I understand your code here. When you do a "return false" or a
"return true" inside the OnClientClick, what effect will that end up having?
Does "return true" result in the Button's server-side "OnClick" function
getting called and the "return false" does not? Is that what happens?

Alex
 
G

Guest

Also, an <ASP:Button /> doesn't seem to have an OnClientClick property. Is it
officially supported?

Alex
 
S

Steven Cheng[MSFT]

Thanks for Teemu's good suggestion.

Hi Alex,

The "OnClientClick" is a new property added in the .net framework 2.0. And
in 1.x, we may need to use codebehind code to programmatically register
such script. e.g:

Button1.Attributes["onclick"] = "if(!confirm('Are you sure?')){return
false};";

And as for the following script:

if(!confirm('Are you sure?'))return false;

you can just expand it to make it looks clearer:


if(!confirm('Are you sure?'))
{
return false;
}

that means if the user click "No" for the confirm javascript dialog, the
code block return false. And return false in client-script event handler
means cancel the current event processing, and for your submit button, it
means stop the postback to server.

Here are some additional web artcles on client script event handling:

#Confirm Delete - Javascript
http://davidhayden.com/blog/dave/archive/2004/03/16/178.aspx

#Advanced JavaScript Event Handling
http://www.webdevelopersjournal.com/articles/jsevents2/jsevents2.html


Hope this also helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top