views:

207

answers:

2

Hello,

I want to trigger on click of a button this:

<asp:Button runat="server" ID="submit" Text="Submit" OnClientClick="country(this.form);" PostBackUrl="http://www.google.com/" /> 

In javascript I m checking validation of the form.The problem is when I m clicking on the button it is not waiting for the validation but its postback to google.com...

If I do return country(this.form) then on button click it waits for validation but don't postback after I fill the form. I want something like that if javascript validation is false..then OnClientClick should be return country(this.form) if its true then only

country(this.form)
+1  A: 

Write it with return:

<asp:Button runat="server" ID="submit" Text="Submit" OnClientClick="return country(this.form);" PostBackUrl="http://www.google.com/" /> 

And your country function will end with return true or false,

function country(form) {
   // Validations goes here
   return true;// or return false;
}
Amr ElGarhy
This won't work...the postback won't fire in either true or false.
Nick Craver
Thats what I said Amr... that when I m doing return I am not able to postback ...
TSSS22
i suppose that the country function return true or false
Amr ElGarhy
@Amr - I updated my answer, you can see why this approach doesn't work in this case.
Nick Craver
i tested it and its working, if the country function return true the post back calls, and if false it do nothing.
Amr ElGarhy
+3  A: 

You can do this:

<asp:Button runat="server" ID="submit" Text="Submit" 
               OnClientClick="if(!country(this.form)) return false;" 
               PostBackUrl="http://www.google.com/" />

Since postbacks use a "onclick" overall, your script is perpending to the ASP.Net script, returning means none of that postback script runs. If you use an if and only return out if needed, it'll work.

It makes more sense when you look at the rendered result, something like this:

<input type="submit" name="submit" value="Submit" id="submit" onclick="if(!country(this.form)) return false; WebForm_DoPostBackWithOptions(.....)" />
Nick Craver
Do i need to mention anything in my javascript that return true or something...because Its not working...my javscript check for validation only...its like.function country(frm){//checks validationfrm.submit}
TSSS22
@ps123 - Yes, `return true` if it validates, `false` if not in your function.
Nick Craver
Right now its checking for validation but not doing validation...means working like as I was doing with return country(this.form)
TSSS22
@ps123 - I'm not sure what you mean, can you post your `country` function?
Nick Craver
yes please show us your country function
Amr ElGarhy
Its working fine...thanks....yah i was not doing return true...
TSSS22
and my solution will work as well
Amr ElGarhy