您的位置:首页 > 其它

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

2014-07-09 22:17 197 查看
背景:维护一个表格时,当用户提交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.";
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐