Confirm box in .NET

M

Mike Malter

I have scoured the web looking for an example of how to do a javascript confirm box in .NET.

Is there any way to do it?

Thanks.
 
M

[MSFT]

Hi Mike,

Regarding the issue, we can call window.confirm method in client side
script. For example:

if(window.confirm("Are you sure that you want to post data back to the
Server?"))
{
}
else
{
}

Does this answer your question?

Luke
Microsoft Online Support

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

Saravana [MVP]

For showing message box in ASP.NET, you need to use client side scripts
only. You can use registerstartupscript or registerclientscript method to
register your client scripts in your form. In the client script you can use
window.confirm to show message box with ok/cancel button. Another option for
showing message box in ASP.NET is, by using free messagebox control which is
available at following location which you can download and use it. Check out
this url's for more details.

www.extremeexperts.com

or
http://www.microsoft.com/india/msdn/articles/119.aspx
 
M

Mike Malter

Thanks for the answer, but how do I do it?

Is this a javascript deal or does it live in my code behind page?

How do I deal with this in my code behind page?

Would it be possible for you to put up a code snippet for me? Basically I have a delete button and I am trapping the click in the
code behind page, and I want the user to confirm.

Thanks.
 
M

Matt Berther

Hello Mike,
I have scoured the web looking for an example of how to do a
javascript confirm box in .NET.

control.Attributes.Add("onClick", "return confirm('Your Message');");

where control is the control you want the confirm box attached to. Keep in mind that this will not postback unless the user selects OK.
 
M

Mike Malter

Matt,

This looks like what I am after, I'll try it. Thanks alot. Also, thanks to the others who answered here too.
 
H

Hans Kesting

Matt Berther said:
Hello Mike,


control.Attributes.Add("onClick", "return confirm('Your Message');");

where control is the control you want the confirm box attached to. Keep in mind that this will not postback unless the user selects OK.


For a regular button this will work, for a linkbutton you will need

control.Attributes.Add("onClick", "if (!confirm('Your Message')) return false;");

because asp.net will add it's own onclick handler after this, to get the postback
to work. That's why you *need* the closing ";" here.

Hans Kesting
 
M

Mike Malter

Guys,

When I do this I get the following compiler error:

C:\Projects\SSSC\Cheese\wwwroot\ManageBrand.aspx.cs(51): Invalid token '(' in class, struct, or interface member declaration

My code is as follows:
protected System.Web.UI.WebControls.Button bDelete;
bDelete.Attributes.Add("onClick", "if (!confirm('Are you sure you want to delete?')) return false;");

What am I doing wrong here?

Thanks.
 
M

Mike Malter

Sorry, I was doing this out of scope. Got it working now and it is great. THANKS!

Mike
 
M

Matt Berther

Hello Mike,
C:\Projects\SSSC\Cheese\wwwroot\ManageBrand.aspx.cs(51): Invalid token
'(' in class, struct, or interface member declaration

My code is as follows:

protected System.Web.UI.WebControls.Button bDelete;

bDelete.Attributes.Add("onClick", "if (!confirm('Are you sure you want
to delete?')) return false;");

My guess is that this has something to do with the sorrounding code, as this block looks fine. Could you post the rest of the method?

As a side note,
if (!confirm('Are you sure you want to delete?')) return false;
is actually the same as
return confirm('Are you sure you want to delete?');

and possibly much clearer to read.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top