• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 330
  • Last Modified:

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'");

0
michael1174
Asked:
michael1174
  • 4
  • 3
1 Solution
 
Michel PlungjanIT ExpertCommented:
btnExit.Attributes.Add("onClick", "if(!confirm('" + MessageToDisplay + "')) window.location='Search.aspx'; return false");
0
 
michael1174Author Commented:
mplugjan, try this but it keeps refreshing the page...

I have my btnExit defined as:

protected System.Web.UI.WebControls.Button btnExit;
0
 
Michel PlungjanIT ExpertCommented:
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 }");

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
michael1174Author Commented:
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.

0
 
Michel PlungjanIT ExpertCommented:
erm yes. that is what the ! (not) does - so if you cancel the confirm it will execute the script

remove it and it will reverse


Add("onClick", "if(confirm('" + MessageToDisplay + "')) { window.location='Search.aspx'; return false }");
0
 
michael1174Author Commented:
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 }");
0
 
Michel PlungjanIT ExpertCommented:
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now