Link to home
Start Free TrialLog in
Avatar of michael1174
michael1174Flag for United States of America

asked on

Confirmation box using javascript and C# ASP.NET

Can someone help me get this confirmation box to work in javascript and c#?  Right now its refreshing the page.  I am putting it on my page load event.  Also, can someon give me an example using this same code to instead of redirecting the user to another page to execute a function instead.  Thanks!

string MessageToDisplay = "Do you want to exit?";
btnExit.Attributes.Add("onClick", "javascript:if(!confirm('" + MessageToDisplay + "')) window.location='Search.aspx'");

Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

btnExit.Attributes.Add("onClick", "if(!confirm('" + MessageToDisplay + "')) window.location='Search.aspx'; return false");
Avatar of michael1174

ASKER

mplugjan, try this but it keeps refreshing the page...

I have my btnExit defined as:

protected System.Web.UI.WebControls.Button btnExit;
1. It should be

Add("onClick", "if(!confirm('" + MessageToDisplay + "')) { window.location='Search.aspx'; return false }");

Is btnExit a submit button?
If so, you need to return false on the onSubmit instead

Then I suggest whatever it takes to get the form to do

.....Add("onSubmit", "if(!confirm('" + MessageToDisplay + "')) { window.location='Search.aspx'; return false }");

Its not a submit button... i used your

Add("onClick", "if(!confirm('" + MessageToDisplay + "')) { window.location='Search.aspx'; return false }");

but its doing to opposite of what I expected.. cancel goes to the search page, OK says on the page.

ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok, thanks... one other thing... instead of redirecting to another page, how do i call a c# function called AddCoverages in javascript?

basically want to accomplish this:

Add("onClick", "if(confirm('" + MessageToDisplay + "')) { call AddCoverages; return false }");
I have no idea unless it involves submitting the form

Here is a tutorial in how to postback from javascript

http://www.dotnetspider.com/kb/Article1939.aspx