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)
最后一记的是在.net里,script里用getElementById不能直接打ID的名字,要用
另外几个特别的取ID方法,取checkboxList的里面一个个的:
问题:这样一来,有的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)
CompareValidator | Compares the value of one input control to the value of another input control or to a fixed value |
CustomValidator | Allows you to write a method to handle the validation of the value entered |
RangeValidator | Checks that the user enters a value that falls between two values |
RegularExpressionValidator | Ensures that the value of an input control matches a specified pattern |
RequiredFieldValidator | Makes an input control a required field |
!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.";
相关文章推荐
- .NET Remoting Basic(10)-创建不同宿主的客户端与服务器端
- 开发ASP.NET Atlas服务器端Extender控件——编写客户端Behavior
- Client Side Validation with JavaScript in ASP.NET
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(8):服务器端和客户端数据类型的自动转换:数组类型
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(10):服务器端和客户端数据类型的自动转换:以XML方式序列化数据、小结
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(10):服务器端和客户端数据类型的自动转换:以XML方式序列化数据、小结
- 在ASP.NET的服务器端使用message box(Message box Server side in ASP.Net)
- 现存问题以及解决方案:在ASP.NET AJAX中从客户端向服务器端传送DataTable
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(6):服务器端和客户端数据类型的自动转换:复杂类型
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(5):服务器端和客户端数据类型的自动转换:基本类型和枚举类型
- asp.net相关控件的客户端及服务器端的禁用和启用
- asp.net中服务器端控件和客户端控件的交互问题
- 客户端调用服务器端方法——ASP.NET AJAX(Atlas)、Anthem.NET和Ajax.NET Professional实现之小小比较
- 客户端调用服务器端方法——ASP.NET AJAX(Atlas)、Anthem.NET和Ajax.NET Professional实现之小小比较
- 现存问题以及解决方案:在ASP.NET AJAX中从客户端向服务器端传送DataTable
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(7):服务器端和客户端数据类型的自动转换:泛型集合类型
- ASP.NET中常用服务器端与客户端交互!
- Microsoft ASP.NET AJAX 使用客户端调用服务器端的方法
- Using the Enterprise Library Validation Application Block in ASP.NET - Part I
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(9):服务器端和客户端数据类型的自动转换:DataTable和DataSet