2

I have a Button with PostBackUrl. Also I have assigned a function to its onclientclick method in which some page logic were written including a couple of validation rules. if whole page were not valid this method returns False but I can not stop page. I have tried document.execCommand('Close') and Window.Stop() but no success.

Any suggestions?

Edit: Button:

<asp:Button ID="btnSubmit" runat="server" OnClientClick="return DoLogic()"  PostBackUrl="http://google.com" Class="btn_submit"/>

DoLogic:

function DoLogic() {

if (ValidateForm()) {
blah blah blah
return true;
}
else {
alert("Has Error");
return false;
}

ValidateForm:

function ValidateForm(){
if (haserror)  
{  
return false; 
} 
return true; 
}
amir moradifard
  • 353
  • 1
  • 8
  • 26

2 Answers2

3

You can use .preventDefault() method on your event. This will stop the default action of a page load when the button is clicked.

http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-cancelation

You can also return false; inside your DoLogic(). This will stop the page load as well.

onclientclick="return DoLogic()"

Then in your function:

function DoLogic(){
    //blah blah blah
    return false;
}

UPDATE

If you're doing all of this and are still having problems, try adding this to your button:

onclick="javascript:return false"

UPDATE FROM OP

Your code isn't working because in the if statement you return true; and false only gets returned if the shit hits the fan (ie an error). You need to return false everytime. OR use the .preventDefault() like I mentioned earlier.

Jonny Sooter
  • 2,417
  • 1
  • 24
  • 40
1

I've read that the PostBackUrl can give you trouble, and especially in this case because it looks like you're using it wrong. I'm thinking you may be confused about what PostBackUrl does. It is an attribute used in Cross-Page posting. If you are trying to redirect the user to a different page when the postback is completed, use Response.Redirect() in the code-behind or use an <asp:HyperLink> tag instead.

There should be no reason for which you could not use Response.Redirect(). Just add it to the end of your script in the code-behind. If the client-side validation returns false, the life-cycle will not reach the server-side anyway.

All-in-all it looks like you have at least three errors in that control:

  1. You definitely need the return part of the OnClientClick attribute.
  2. You need to change the Class attribute to CssClass.
  3. You probably need to use something other than PostBackUrl on that button.

You can read more about it here:


To elaborate . . .

If you are trying to send a user to another site when the code is finished executing, the PostBackUrl attribute cannot be used in the manner you have shown us. For instance, in your example, even if it did work, the user would likely see this:

enter image description here

Ross Brasseaux
  • 3,879
  • 1
  • 28
  • 48
  • Neither of Response.redirect or Hyperlink is an option here. I have to use PostBackURL. – amir moradifard Jul 24 '13 at 21:48
  • I have updated the question. Could you please take a look and guess what could be wrong here? – amir moradifard Jul 24 '13 at 21:50
  • @amirmoradifard Are you trying to redirect the user to a URL or did you replace your local file path with "http://google.com" for the purposes of this question? – Ross Brasseaux Jul 24 '13 at 22:00
  • I have changed it to google. Actually the code is Clicking the button should post form to WorldPay website. So redirect and Hyperlink can not be used for this purpose. – amir moradifard Jul 25 '13 at 03:40