Confirmation Message

D

Dave

Hello.
I want to prompt user for some action with Yes/No message, I know some
tricks in JavaScript, but I need to handle the user answer at server side.
Exactly I want to prompt user for confirmation on delete action of some data
in my application.
How can I do this?
 
S

Sahil Malik

If you look in the generated html out of an asp.net page, it contains a
form, and that form is submitted by a standard javascript function. You need
to write your custom javascript that ends up calling that javascript
function (which then does the postback).

Hope that helped.

- Sahil Malik
You can reach me thru my blog -
http://www.dotnetjunkies.com/weblog/sahilmalik
 
K

Ken Dopierala Jr.

Hi,

Here is how I would do it. First create your button that launches the
Yes/No message, then declare it in your code behind. For example:

Protected WithEvents btnDelete As System.Web.UI.WebControls.Button

Then add this attribute:

btnDelete.Attributes.Add("onclick", "return ConfirmDelete();")

In your ASPX page add this javascript function:

function ConfirmDelete() {
boolReturn = confirm("Are you sure you wish to delete this?");
if (boolReturn)
return true;
else
return false;
}

Then create your handler in your code behind to do the deletion for
btnDelete.

The attribute you add calls the client side javascript function to confirm
the user's choice. If the user confirms it, then true is returned which
will submit the form and run your btnDelete click event. Should the user
cancel, then false is returned and the form will not post back. Good luck!
Ken.
 
D

Dave

Can you show me some demo please?

Ken Dopierala Jr. said:
Hi,

Here is how I would do it. First create your button that launches the
Yes/No message, then declare it in your code behind. For example:

Protected WithEvents btnDelete As System.Web.UI.WebControls.Button

Then add this attribute:

btnDelete.Attributes.Add("onclick", "return ConfirmDelete();")

In your ASPX page add this javascript function:

function ConfirmDelete() {
boolReturn = confirm("Are you sure you wish to delete this?");
if (boolReturn)
return true;
else
return false;
}

Then create your handler in your code behind to do the deletion for
btnDelete.

The attribute you add calls the client side javascript function to confirm
the user's choice. If the user confirms it, then true is returned which
will submit the form and run your btnDelete click event. Should the user
cancel, then false is returned and the form will not post back. Good
luck!
Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.
 
K

Ken Dopierala Jr.

Hi,

I don't have a demo, all the code I gave you will work. Just drag a button
from the toolbox onto your form. Name it btnDelete. Then in your page load
event put this:

btnDelete.Attributes.Add("onclick", "return ConfirmDelete();")

In the <head> section of your HTML put this:

<script language="javascript">
function ConfirmDelete() {
boolReturn = confirm("Are you sure you wish to delete this?");
if (boolReturn)
return true;
else
return false;
}
</script>

Go back to your code page. Select the btnDelete control from the dropdown
on the left just above your code and below your tabs. Then in the drop down
directly to the right select the Click event. Set a break point on the
first line of the btnDelete_Click() function that is created for you. Now
run your app.

When you click the button on your form, a window will popup asking you if
you want to confirm. If you click OK it will go back to the server and hit
your break point. If you click Cancel, the confirmation will disappear and
nothing will happen. Good luck! Ken.
 
M

Mark Rae

You don't actually need a Javascript function in the header for this:

btnDelete.Attributes.Add("onclick", "return confirm('Are you sure you want
to delete this record?');");

will work just as well
 
K

Ken Dopierala Jr.

Thanks Mark!

Use what Mark has said, it will be much easier! If you ever need to do more
on the client based on a decision then consider what I posted. But to get
you up and running fast this is the way to go! Ken.
 

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,777
Messages
2,569,604
Members
45,217
Latest member
topweb3twitterchannels

Latest Threads

Top