Home > Archive > ASP .NET > April 2006 > Elegant Way to Handle "Are you Sure" Dialog on Client
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Elegant Way to Handle "Are you Sure" Dialog on Client
|
|
| Alex Maghen 2006-04-30, 7:07 pm |
| 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
| |
| Teemu Keiski 2006-04-30, 7:07 pm |
| 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" <AlexMaghen@newsgroup.nospam> wrote in message
news:DB8CDE23-2181-44CA-BD8B-3E6D0EC782BD@microsoft.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
| |
| Alex Maghen 2006-04-30, 7:07 pm |
| Oooh! I didn't know about this OnClientClick thing. That's pretty . 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" <AlexMaghen@newsgroup.nospam> wrote in message
> news:DB8CDE23-2181-44CA-BD8B-3E6D0EC782BD@microsoft.com...
>
>
>
| |
| Alex Maghen 2006-04-30, 7:07 pm |
| 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" <AlexMaghen@newsgroup.nospam> wrote in message
> news:DB8CDE23-2181-44CA-BD8B-3E6D0EC782BD@microsoft.com...
>
>
>
| |
| Alex Maghen 2006-04-30, 7:07 pm |
| Oops! I was wrong. Never mind!
"Alex Maghen" wrote:
[color=darkred]
> Also, an <ASP:Button /> doesn't seem to have an OnClientClick property. Is it
> officially supported?
>
> Alex
>
> "Teemu Keiski" wrote:
>
| |
| Teemu Keiski 2006-04-30, 7:07 pm |
| Yes,
"return false;" cancels the postback. Bit related discussion in my blog:
http://aspadvice.com/blogs/joteke/a...4/30/17118.aspx
--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"Alex Maghen" <AlexMaghen@newsgroup.nospam> wrote in message
news:B0D094AE-7264-4EA3-8B19-758A83D0DE89@microsoft.com...[color=darkred]
> Oooh! I didn't know about this OnClientClick thing. That's pretty .
> 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:
>
| |
| Steven Cheng[MSFT] 2006-04-30, 10:05 pm |
| 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/ar.../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.)
|
|
|
|
|