CustomValidator控件用法
来源:互联网 发布:鹰眼大数据系统怎么样 编辑:程序博客网 时间:2024/05/21 20:28
虽然大部分时间一直从事asp.net的开发,对于一些常用的asp.net服务器端验证控件及它们的组合使用比较熟悉,如:
CompareValidator ——比较验证控件
RangeValidator ——范围验证控件
RegularExpressionValidator ——正则验证控件
RequiredFieldValidator ——必填验证控件
但是一直没去研究CustomValidator控件的用法,心中自然也有一种想法:好不容易从写js验证的痛苦中摆脱出来,如今是能不回去就不要回去了,但是有时候又会遇到一些用上面提到的验证控件没有办法做到的情况(当然是指在客户端没办法做到,我要是说在服务器端没办法做到就让大家笑掉大牙了),用自然想它在客户端完成验证了,至少也要先在客户端验证一下,通过客户端验证再到服务器端验证,这样减少数据的往返时间,降低网络流量和保证反应及时性。
CustomValidator 是一个提供灵活验证方式的控件,它也能在客户端和服务器端验证,分别提供了两种验证的方法原型:
服务器端验证:
无论对于客户端验证还是服务器端验证,设置通过验证的办法就是将第二个参数的IsValid属性设置为true即可,反之设置为false。
下面我以两个例子来讲述如何使用CustomValidator 来进行验证,第一个用法是用来验证TextBox,第二个用法是验证CheckBoxList,确保其中只能选中一项,而且必须选中一项,对于TextBox这类的控件,我这里设置了对应的CustomValidator 的ValidateEmptyText="True"属性,这样即使控件值为空也能得到验证。
全部代码如下(这里是cs代码和html代码混合的模式):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="keywords" content="CustomValidator控件用法" />
<meta name="description" content="周公讲述CustomValidator控件用法" />
<title>CustomValidator控件用法</title>
</head>
<script language="javascript" type="text/javascript">
function ClientValidateUserName(source,args)
{
//alert(source);source=CustomValidator1,为验证控件
//alert(args);
var obj=document.getElementById("txtUserName");
if((obj.value=="")||(obj.value.length>10))
{
args.IsValid=false;
}
else
{
args.IsValid=true;
}
}
function CheckCheckBoxList(source,args)
{
var obj=document.getElementById('<%=this.cbAgeRangeList.ID%>');//返回具有和属性id的值相同或相似的对象集合
var k,right=false;
var length=(obj.all.tags('input').length);//返回obj对象里具有“input”标签对象的集合
for(k=0;k<length;k++)
{
//alert(" name:"+obj.all.tags('input')[k].name+"是否:"+obj.all.tags('input')[k].checked);
if(obj.all.tags('input')[k].checked) // obj对象里具有input标签对象键值为k的属性为checked的值
{
right=!right;
}
}
if(right)
{
args.IsValid=true;
}
else
{
args.IsValid=false;
}
}
</script>
<script runat="server">
protected void Page_Load(Object Src, EventArgs E)
{
if (!IsPostBack) DataBind();
}
public void ServerValidateUserName(object source, ServerValidateEventArgs args)
{
if((string.IsNullOrEmpty(txtUserName.Text))||(txtUserName.Text.Length>10))
{
args.IsValid=false;
}
else
{
args.IsValid=true;
}
}
</script>
<body>
<form runat="server">
UserName:<asp:TextBox ID="txtUserName" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="错误信息" ControlToValidate="txtUserName" ClientValidationFunction="ClientValidateUserName" ValidateEmptyText="True"></asp:CustomValidator>
<asp:Button ID="btnValid1" runat="server" Text="验证用户名" />
<asp:CheckBoxList ID="cbAgeRangeList" runat="server" Height="36px" Width="400px" RepeatDirection="Vertical">
<asp:ListItem Value="1"><16</asp:ListItem>
<asp:ListItem Value="2">16-22</asp:ListItem>
<asp:ListItem Value="3">22-30</asp:ListItem>
<asp:ListItem Value="4">30-40</asp:ListItem>
<asp:ListItem Value="5">40-50</asp:ListItem>
<asp:ListItem Value="6">50-60</asp:ListItem>
<asp:ListItem Value="6">60-80</asp:ListItem>
<asp:ListItem Value="6">>80</asp:ListItem>
</asp:CheckBoxList>
<asp:customvalidator id="CustomValidator2" runat="server"
ErrorMessage="请正确选择年龄段" ClientValidationFunction="CheckCheckBoxList"></asp:customvalidator>
</form>
</body>
</html>
- CustomValidator控件用法
- CustomValidator控件用法
- CustomValidator控件另种用法
- CustomValidator 控件
- CustomValidator控件
- 自定义判断控件customvalidator
- CustomValidator控件的使用
- CustomValidator控件的使用
- customvalidator控件的使用
- CustomValidator自定义验证控件
- Validation服务器控件:CustomValidator控件
- 自定义客户端验证控件 CustomValidator
- CustomValidator 控件(常规参考)
- CustomValidator自定义控件的使用
- CustomValidator验证控件 的使用方法
- CustomValidator验证控件 的使用方法
- CustomValidator
- CustomValidator
- 在基于Struts构架的Java Web项目中加入ICTCLAS分词http://blog.csdn.net/CloneIQ/archive/2006/08/09/1043088.aspx
- 笔试题2
- 程序员怎样学数学:半路出家也能让编程是小菜一碟
- 使用PreparedStatement的Batch功能
- eclipse界面语言设定
- CustomValidator控件用法
- Oracle数据库设计开发阶段性能优化策略
- [C++]尽量以const,enum,inline替换#define
- 纪念贴
- 等待窗体的制作
- DB2常用命令汇总之二
- 数据库设计中的14个技巧
- DB2常用命令汇总之三
- Delphi7 无法启动