Validation in .NET 服务器端和客户端的Trigger不同

背景:维护一个表格时,当用户提交submit,有验证输入的功能,目前我在server端(C# code,也就是aspx.vb文件里)写了判断(if-else)来实现让页面返回,也就是停止。

问题:这样一来,有的validation在提交时client端实现,我维护的这个要到服务器才响应,就有先后,不符合用户的使用习惯。


首先搞明白了什么是客户端,服务器端,在html,aspx里面写的是客户端,这些代码不需要到server里相应,当用户提交时及触发(By default the client-side validation is triggered when submitting forms using buttons. 也有例外,customValidator是在用户变化提交时触发,这也就是为什么可以用它做custom的原因),可以用asp.net里的各种validator和镶嵌的javascript实现。

服务器端就是C#的代码,响应就要慢一半拍。

各种validator: (http://www.w3schools.com/aspnet/aspnet_refvalidationcontrols.asp)

CompareValidatorCompares the value of one input control to the value of another input control or to a fixed value
CustomValidatorAllows you to write a method to handle the validation of the value entered
RangeValidatorChecks that the user enters a value that falls between two values
RegularExpressionValidatorEnsures that the value of an input control matches a specified pattern
RequiredFieldValidatorMakes an input control a required field

最后一记的是在.net里,script里用getElementById不能直接打ID的名字,要用

!document.getElementById("<%=forOthers.ClientID %>").checked
因为,HTML的id attribute被自动设到ClientID属性中,Reference: http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid%28v=vs.110%29.aspx

另外几个特别的取ID方法,取checkboxList的里面一个个的:

document.getElementById("<%=cbPurpose.ClientID %>").getElementsByTagName("input")[0].checked
当读取的时候,基本用checked,value这样的属性,当写的时候,可以写innerHTML,比如说写label的text:
	            document.getElementById("<%=lblCheckPurpose.ClientID %>").innerHTML = "Enter the Deactive Port Details.";

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值