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("<script language=/"javascript/">/n");
sb_Script.Append("function cb_vefify(val) {/n");
sb_Script.Append("var isValid=false;/n");
sb_Script.Append("var val = document.all[document.all[/""+this.ID+"/"].controltovalidate];/n");
sb_Script.Append("var col = val.all;/n");
sb_Script.Append("if ( col != null )/n");
sb_Script.Append("for ( i = 0; i < col.length; i++ ) /n");
sb_Script.Append("if (col.item(i).tagName == /"INPUT/") /n");
sb_Script.Append("if ( col.item(i).checked )isValid=true; /n");
sb_Script.Append("return isValid;");
sb_Script.Append("}/n");
sb_Script.Append("</script>");
this.Page.RegisterClientScriptBlock("RBLScript", sb_Script.ToString());
//通过设置验证器的evaluationfunction attribute来指定其客户端验证逻辑
this.Attributes["evaluationfunction"] = "cb_vefify";
}
}
}
新闻热点
疑难解答
图片精选