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

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

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


P: n/a
Hi,

using OnClientClick attribute of Button

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

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"Alex Maghen" <Al********@newsgroup.nospam> wrote in message
news:DB**********************************@microsof t.com...
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

Apr 30 '06 #2

P: n/a
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
"Teemu Keiski" wrote:
Hi,

using OnClientClick attribute of Button

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

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"Alex Maghen" <Al********@newsgroup.nospam> wrote in message
news:DB**********************************@microsof t.com...
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


Apr 30 '06 #3

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

Alex

"Teemu Keiski" wrote:
Hi,

using OnClientClick attribute of Button

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

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"Alex Maghen" <Al********@newsgroup.nospam> wrote in message
news:DB**********************************@microsof t.com...
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


Apr 30 '06 #4

P: n/a
Oops! I was wrong. Never mind!

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

Alex

"Teemu Keiski" wrote:
Hi,

using OnClientClick attribute of Button

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

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"Alex Maghen" <Al********@newsgroup.nospam> wrote in message
news:DB**********************************@microsof t.com...
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


Apr 30 '06 #5

P: n/a
Yes,

"return false;" cancels the postback. Bit related discussion in my blog:
http://aspadvice.com/blogs/joteke/ar.../30/17118.aspx

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

"Alex Maghen" <Al********@newsgroup.nospam> wrote in message
news:B0**********************************@microsof t.com...
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
"Teemu Keiski" wrote:
Hi,

using OnClientClick attribute of Button

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

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"Alex Maghen" <Al********@newsgroup.nospam> wrote in message
news:DB**********************************@microsof t.com...
> 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


Apr 30 '06 #6

P: n/a
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/arc...03/16/178.aspx

#Advanced JavaScript Event Handling
http://www.webdevelopersjournal.com/...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.)
May 1 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.