开发者

problem with onclientclick and postbackurl

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 开发者_C百科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)


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(.....)" />


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜