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

本文介绍如何在ASP.NET中正确实现客户端验证,确保只有当客户端验证通过时才会触发服务器端代码执行,从而有效避免不必要的服务器请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

昨天做了这么个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">的客户端验证功能,
以避免不必要的服务器请求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值