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基類的自定義驗(yàn)證器
public class RequiredCheckBoxListValidator : BaseValidator {
private ListControl m_ListCtl;
//啟用客戶端驗(yàn)證
public RequiredCheckBoxListValidator(){
base.EnableClientScript = true;
}
//確定由ControlToValidate指定的被驗(yàn)證控件是否有效
protected override bool ControlPropertiesValid(){
Control webControl = this.FindControl(ControlToValidate);
if (webControl != null){
m_ListCtl = (ListControl)webControl;
return true;
}
else
return false;
}
//定義校驗(yàn)規(guī)則
protected override bool EvaluateIsValid(){
return (m_ListCtl.SelectedIndex != -1);
}
protected override void OnPreRender(EventArgs e) {
if (this.EnableClientScript) this.GenerateScript();
base.OnPreRender(e);
}
//產(chǎn)生并向頁(yè)面注入客戶端驗(yàn)證腳本
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());
//通過(guò)設(shè)置驗(yàn)證器的evaluationfunction attribute來(lái)指定其客戶端驗(yàn)證邏輯
this.Attributes["evaluationfunction"] = "cb_vefify";
}
}
}
新聞熱點(diǎn)
疑難解答
圖片精選