Confirm box in .NET

  • Thread starter Thread starter Mike Malter
  • Start date Start date
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.
 
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.)
 
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
 
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.
 
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.
 
Matt,

This looks like what I am after, I'll try it. Thanks alot. Also, thanks to the others who answered here too.
 
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
 
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.
 
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.
 
Back
Top