自定义验证控件CustomValidator的用法实例

来源:百度文库 编辑:神马文学网 时间:2024/04/30 01:29:04
Customvalidator用法的实例的前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test01.aspx.cs" Inherits="test01" %>



CustomValidator的用法






OnServerValidate="TextValidate" ControlToValidate="TextBox1" ClientValidationFunction="validateLength" ErrorMessage="Text must
be 8 or more characters." Display="Dynamic">






Customvalidator用法的实例的后台代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class test01 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TextValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = (args.Value.Length >= 8);
//args.IsValid的值等于true,则通过验证,若为false,则没有通过验证
//args.Value的值就是CustomValidator所绑定的控件中的值,此例中是textbox控件中的值
if (args.Value.Length >= 8)
{
RegisterStartupScript("yes1", "");
}
else
{
RegisterStartupScript("yes1", "");
}
}
}