一个Page页面多个CheckBoxList的客户端验证(.net1.1及2.0实现)(downmoon)
一,基于.net1.1
1、类文件

usingSystem;
usingSystem.Web.UI;
usingSystem.Web;
usingSystem.Web.UI.WebControls;
usingSystem.Text;


namespaceAgrExibition.Common.Framework.Util...{


欢迎与邀月交流,net技术与软件架构2007/07/26#regiondownmoon2007/07/26

/**////<summary>
///一个Page页面多个CheckBoxList的客户端验证(.net1.1实现)(downmoon)
///</summary>
#endregion
publicclassRadioButtonListRequiredFieldValidator:BaseValidator

...{

protectedoverrideboolControlPropertiesValid()

...{
returntrue;
}

protectedoverrideboolEvaluateIsValid()

...{
returnthis.EvaluateIsChecked();
}
protectedboolEvaluateIsChecked()

...{
ListControl_listcontrol=((ListControl)this.FindControl(this.ControlToValidate));
foreach(ListItemliin_listcontrol.Items)

...{
if(li.Selected)returntrue;
}
returnfalse;
}

protectedoverridevoidOnPreRender(EventArgse)

...{


if(this.EnableClientScript)...{this.ClientScript();}
base.OnPreRender(e);

}

protectedvoidClientScript()

...{
this.Attributes["evaluationfunction"]="cb_vefify";

StringBuildersb_Script=newStringBuilder();
sb_Script.Append("<scriptlanguage="javascript">");
sb_Script.Append(" ");
sb_Script.Append(" ");
sb_Script.Append("functioncb_vefify(val){");
sb_Script.Append(" ");
sb_Script.Append("varval=document.getElementById(val.controltovalidate);");
sb_Script.Append(" ");
sb_Script.Append("varcol=val.all;");
sb_Script.Append(" ");
sb_Script.Append("if(col!=null){");
sb_Script.Append(" ");
sb_Script.Append("for(i=0;i<col.length;i++){");
sb_Script.Append(" ");
sb_Script.Append("if(col.item(i).tagName.toUpperCase()=="INPUT"){");
sb_Script.Append(" ");
sb_Script.Append("if(col.item(i).checked){");
sb_Script.Append(" ");
sb_Script.Append(" ");
sb_Script.Append("returntrue;");
sb_Script.Append(" ");
sb_Script.Append("}");
sb_Script.Append(" ");
sb_Script.Append("}");
sb_Script.Append(" ");
sb_Script.Append("}");
sb_Script.Append(" ");
sb_Script.Append(" ");
sb_Script.Append(" ");
sb_Script.Append("returnfalse;");
sb_Script.Append(" ");
sb_Script.Append("}");
sb_Script.Append(" ");
sb_Script.Append("}");
sb_Script.Append(" ");
sb_Script.Append("</script>");
if(!this.Page.IsClientScriptBlockRegistered("RBLScript"))

...{
this.Page.RegisterClientScriptBlock("RBLScript",sb_Script.ToString());
}

}

}

}

2、页面

<%...@RegisterTagPrefix="AgrExibition"Namespace="AgrExibition.Common.Framework.Util"Assembly="AgrExibition.Common.Framework"%>
<asp:CheckBoxListid="cbB_Property"runat="server"RepeatDirection="Horizontal"RepeatColumns="3">
<asp:ListItemValue="1">投资商</asp:ListItem>
<asp:ListItemValue="2">投资促进机构</asp:ListItem>
<asp:ListItemValue="3">代理商</asp:ListItem>
<asp:ListItemValue="4">科研及信息单位协会</asp:ListItem>
<asp:ListItemValue="5">零售商</asp:ListItem>
<asp:ListItemValue="6">批发商</asp:ListItem>
<asp:ListItemValue="7">仓储业</asp:ListItem>
<asp:ListItemValue="8">跨国公司</asp:ListItem>
<asp:ListItemValue="9">进出口商</asp:ListItem>
<asp:ListItemValue="10">生产商</asp:ListItem>
<asp:ListItemValue="11">超市</asp:ListItem>
<asp:ListItemValue="12">酒店</asp:ListItem>
<asp:ListItemValue="13">宾馆及后勤服务</asp:ListItem>
<asp:ListItemValue="-1">其他</asp:ListItem>
</asp:CheckBoxList>
<asp:TextBoxid="txtB_Property"Runat="Server"CssClass="input2"Width="400px"/>
<AgrExibition:RADIOBUTTONLISTREQUIREDFIELDVALIDATORid="rbValid1"Display="Dynamic"ErrorMessage="请选择或输入业务性质!"
runat="server"ControlToValidate="cbB_Property"Text="*">*</AgrExibition:RADIOBUTTONLISTREQUIREDFIELDVALIDATOR>
<br/>
<asp:CheckBoxListid="cbPurpose"runat="server"RepeatDirection="Horizontal"RepeatColumns="3">
<asp:ListItemValue="1">采购产品</asp:ListItem>
<asp:ListItemValue="2">搜集市场信息</asp:ListItem>
<asp:ListItemValue="3">与业务伙伴会面</asp:ListItem>
<asp:ListItemValue="4">了解行业动向</asp:ListItem>
<asp:ListItemValue="5">观看观摩</asp:ListItem>
<asp:ListItemValue="6">投资项目洽谈</asp:ListItem>
<asp:ListItemValue="7">获取行业信息</asp:ListItem>
<asp:ListItemValue="8">寻找供应商或买家</asp:ListItem>
<asp:ListItemValue="-1">其他</asp:ListItem>
</asp:CheckBoxList>
<asp:TextBoxid="txtPurpose"Runat="Server"CssClass="input2"Width="400px"/>
<AgrExibition:RADIOBUTTONLISTREQUIREDFIELDVALIDATORid="rbValid2"Display="Dynamic"ErrorMessage="请选择或输入参会目的!"
runat="server"ControlToValidate="cbPurpose"Text="*">*</AgrExibition:RADIOBUTTONLISTREQUIREDFIELDVALIDATOR>
<asp:ValidationSummaryid="ValidationSummary1"runat="server"ShowMessageBox="True"ShowSummary="False"
HeaderText="请检查输入表单内容:"></asp:ValidationSummary>
二、基于.net 2.0
1、类文件
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Text;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;

namespaceShiXun.Commoon.Web.Validor

...{


欢迎与邀月交流,net技术与软件架构2007/07/26#regiondownmoon2007/07/26

/**////<summary>
///一个Page页面多个CheckBoxList的客户端验证(.net2.0实现)(downmoon)
///</summary>
#endregion

[DefaultProperty("ErrorMessage"),ToolboxData("<{0}:ListControlRequiredFieldValidatorrunat=server></{0}:ListControlRequiredFieldValidator>")]
publicclassListControlRequiredFieldValidator:BaseValidator

...{

/**////<summary>
///ValidatorRequirement
///</summary>

///<returns>Trueifdependenciesarevalid.</returns>
protectedoverrideboolControlPropertiesValid()

...{
ControlcontrolToValidate=FindControl(ControlToValidate)asListControl;
return(controlToValidate!=null);
}


/**////<summary>
///ValidatorRequirement
///</summary>
///<returns>trueifControlToValidate
///hasoneitemormoreselected</returns>

protectedoverrideboolEvaluateIsValid()

...{
returnthis.EvaluateIsChecked();
}


/**////<summary>
///Returntrueifaniteminthelistisselected.
///</summary>
///<returns>trueifControlToValidate
///hasoneitemormoreselected</returns>
protectedboolEvaluateIsChecked()

...{
ListControllistToValidate=((ListControl)this.FindControl(this.ControlToValidate));

foreach(ListItemliinlistToValidate.Items)

...{
if(li.Selected==true)
returntrue;
}
returnfalse;
}


/**////<summary>

///PreRender
///</summary>
///<paramname="e"></param>
protectedoverridevoidOnPreRender(EventArgse)

...{
System.Web.HttpContext.Current.Trace.Write("OverrideOnPreRender");
if(this.DetermineRenderUplevel()&&this.EnableClientScript)

...{
Page.ClientScript.RegisterExpandoAttribute(this.ClientID,"evaluationfunction","ListItemVerify");
Page.ClientScript.RegisterExpandoAttribute(this.ClientID,"minimumNumberOfSelectedCheckBoxes","1");

//System.Web.UI.Page.(this.ClientID,"evaluationfunction","ListItemVerify");
//Page.ClientScript.RegisterExpandoAttribute(this.ClientID,"minimumNumberOfSelectedCheckBoxes","1");


//TODO:imporovetoallowvariablenumber.
this.RegisterClientScript();
}
else

...{
this.Attributes.Remove("evaluationfunction");
}
base.OnPreRender(e);
}


/**////<summary>
///Registertheclientscript.
///</summary>

protectedvoidRegisterClientScript()

...{
stringscript=@"

<scriptlanguage=""javascript"">
functionListItemVerify(val)
{
varcontrol=document.getElementById(val.controltovalidate);
varminimumNumberOfSelectedCheckBoxes=parseInt(val.minimumNumberOfSelectedCheckBoxes);
varselectedItemCount=0;
varliIndex=0;
varcurrentListItem=document.getElementById(control.id+'_'+liIndex.toString());
while(currentListItem!=null)
{
if(currentListItem.checked)selectedItemCount++;
liIndex++;
currentListItem=document.getElementById(control.id+'_'+liIndex.toString());
}
returnselectedItemCount>=minimumNumberOfSelectedCheckBoxes;
}
</script>
";

this.Page.ClientScript.RegisterClientScriptBlock(typeof(ListControlRequiredFieldValidator),"ListRequiredValidator_Script",script);
}

}
}

2、页面

<%...@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>

<%...@RegisterTagPrefix="ShiXun"NameSpace="ShiXun.Commoon.Web.Validor"Assembly="ShiXun.Commoon.Web.Validor"%>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>无标题页</title>
</head>
<body>
<formid="form1"runat="server">
<asp:CheckBoxListid="cbB_Property"runat="server"RepeatDirection="Horizontal"RepeatColumns="3">
<asp:ListItemValue="1">投资商</asp:ListItem>
<asp:ListItemValue="2">投资促进机构</asp:ListItem>
<asp:ListItemValue="3">代理商</asp:ListItem>
<asp:ListItemValue="4">科研及信息单位协会</asp:ListItem>
<asp:ListItemValue="5">零售商</asp:ListItem>
<asp:ListItemValue="6">批发商</asp:ListItem>
<asp:ListItemValue="7">仓储业</asp:ListItem>
<asp:ListItemValue="8">跨国公司</asp:ListItem>
<asp:ListItemValue="9">进出口商</asp:ListItem>
<asp:ListItemValue="10">生产商</asp:ListItem>
<asp:ListItemValue="11">超市</asp:ListItem>
<asp:ListItemValue="12">酒店</asp:ListItem>
<asp:ListItemValue="13">宾馆及后勤服务</asp:ListItem>
<asp:ListItemValue="-1">其他</asp:ListItem>
</asp:CheckBoxList>
<ShiXun:ListControlRequiredFieldValidator
ControlToValidate="cbB_Property"
display="Dynamic"
ErrorMessage="至少选择一项!"
EnableClientScript="true"
runat="Server">
至少选择一项
</ShiXun:ListControlRequiredFieldValidator>
<br/>
<br/>
<asp:CheckBoxListID="cbPurpose"runat="server"RepeatColumns="3"RepeatDirection="Horizontal">
<asp:ListItemValue="1">采购产品</asp:ListItem>
<asp:ListItemValue="2">搜集市场信息</asp:ListItem>
<asp:ListItemValue="3">与业务伙伴会面</asp:ListItem>
<asp:ListItemValue="4">了解行业动向</asp:ListItem>
<asp:ListItemValue="5">观看观摩</asp:ListItem>
<asp:ListItemValue="6">投资项目洽谈</asp:ListItem>
<asp:ListItemValue="7">获取行业信息</asp:ListItem>
<asp:ListItemValue="8">寻找供应商或买家</asp:ListItem>
<asp:ListItemValue="-1">其他</asp:ListItem>
</asp:CheckBoxList>
<ShiXun:ListControlRequiredFieldValidator
ControlToValidate="cbPurpose"
display="Dynamic"
ErrorMessage="至少选择一项!"
EnableClientScript="true"
runat="Server">
至少选择一项
</ShiXun:ListControlRequiredFieldValidator>
<br/>
<asp:CheckBoxListID="cbChannel"runat="server"RepeatColumns="3"RepeatDirection="Horizontal">
<asp:ListItemValue="1">报纸等平面媒体</asp:ListItem>
<asp:ListItemValue="2">网络</asp:ListItem>
<asp:ListItemValue="3">电视</asp:ListItem>
<asp:ListItemValue="4">朋友介绍</asp:ListItem>
<asp:ListItemValue="5">客户推荐</asp:ListItem>
<asp:ListItemValue="-1">其他</asp:ListItem>
</asp:CheckBoxList>
<ShiXun:ListControlRequiredFieldValidator
ControlToValidate="cbChannel"
display="Dynamic"
ErrorMessage="至少选择一项!"
EnableClientScript="true"
runat="Server">
至少选择一项
</ShiXun:ListControlRequiredFieldValidator>
<asp:TextBoxID="txtSourceChannel"runat="Server"CssClass="input2"Width="400px"></asp:TextBox>
<br/>
<br/>
<br/>
<asp:ButtonID="Button1"runat="server"Text="Button"/>
</form>
</body>
</html>
