Button的客户端验证(client-side validate of ASP.NET button)

昨天做了这么个case

客户的提问:

Hi,

I would like to check using javascript if the client side validation is true and then only allow the onclick event to be fired and the server side code to be executed.

 

< asp:Button  OnClientClick =   " if (Page_ClientValidate()) return true; else return false; "   OnClick = " nameofMethod "  CausesValidation = " True "   />

 

 I have tried the above code but the server side method is executed even if the client side validation is false.

Thanks


我的回答:

Hi denniscy

try to use this,it works on my machine

 

OnClientClick = " return checkForm(); " // don' t forget the ";"

bool checkForm()
{
if (Page_ClientValidate()) return trueelse return false;
}


在实际应用中,使用.net提供的OnClientClick同样可以做到<input type="button" runat="server">的客户端验证功能,
以避免不必要的服务器请求。

你可能感兴趣的:(validate)