Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)delvpublished in(发表于) 2014/1/6 9:03:02 ASP.NET入门随想之检票的老太太_[Asp.Net教程]
using System;using System.Collections;using System.Text;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.ComponentModel;namespace essay.Controls{ //派生于BaseValidator基类的自定义验证器 public class RequiredCheckBoxListValidator : BaseValidator { private ListControl m_ListCtl; //启用客户端验证 public RequiredCheckBoxListValidator(){ base.EnableClientScript = true; } //确定由ControlToValidate指定的被验证控件是否有效 protected override bool ControlPropertiesValid(){ Control webControl = this.FindControl(ControlToValidate); if (webControl != null){ m_ListCtl = (ListControl)webControl; return true; } else return false; } //定义校验规则 protected override bool EvaluateIsValid(){ return (m_ListCtl.SelectedIndex != -1); } protected override void OnPreRender(EventArgs e) { if (this.EnableClientScript) this.GenerateScript(); base.OnPreRender(e); } //产生并向页面注入客户端验证脚本 protected void GenerateScript(){ StringBuilder sb_Script = new StringBuilder(); sb_Script.Append(""); this.Page.RegisterClientScriptBlock("RBLScript", sb_Script.ToString()); //通过设置验证器的evaluationfunction attribute来指定其客户端验证逻辑 this.Attributes["evaluationfunction"] = "cb_vefify"; } }}
赞