一,基于.net1.1
1、类文件
using
System;
using
System.Web.UI;
using
System.Web;
using
System.Web.UI.WebControls;
using
System.Text;

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

<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
runat
="server"
>
<
title
>
无标题页
</
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
asp:CheckBoxList
id
="cbB_Property"
runat
="server"
RepeatDirection
="Horizontal"
RepeatColumns
="3"
>
<
asp:ListItem
Value
="1"
>
投资商
</
asp:ListItem
>
<
asp:ListItem
Value
="2"
>
投资促进机构
</
asp:ListItem
>
<
asp:ListItem
Value
="3"
>
代理商
</
asp:ListItem
>
<
asp:ListItem
Value
="4"
>
科研及信息单位协会
</
asp:ListItem
>
<
asp:ListItem
Value
="5"
>
零售商
</
asp:ListItem
>
<
asp:ListItem
Value
="6"
>
批发商
</
asp:ListItem
>
<
asp:ListItem
Value
="7"
>
仓储业
</
asp:ListItem
>
<
asp:ListItem
Value
="8"
>
跨国公司
</
asp:ListItem
>
<
asp:ListItem
Value
="9"
>
进出口商
</
asp:ListItem
>
<
asp:ListItem
Value
="10"
>
生产商
</
asp:ListItem
>
<
asp:ListItem
Value
="11"
>
超市
</
asp:ListItem
>
<
asp:ListItem
Value
="12"
>
酒店
</
asp:ListItem
>
<
asp:ListItem
Value
="13"
>
宾馆及后勤服务
</
asp:ListItem
>
<
asp:ListItem
Value
="-1"
>
其他
</
asp:ListItem
>
</
asp:CheckBoxList
>
<
ShiXun:ListControlRequiredFieldValidator
ControlToValidate
="cbB_Property"
display
="Dynamic"
ErrorMessage
="至少选择一项!"
EnableClientScript
="true"
runat
="Server"
>
至少选择一项
</
ShiXun:ListControlRequiredFieldValidator
>
<
br
/>
<
br
/>
<
asp:CheckBoxList
ID
="cbPurpose"
runat
="server"
RepeatColumns
="3"
RepeatDirection
="Horizontal"
>
<
asp:ListItem
Value
="1"
>
采购产品
</
asp:ListItem
>
<
asp:ListItem
Value
="2"
>
搜集市场信息
</
asp:ListItem
>
<
asp:ListItem
Value
="3"
>
与业务伙伴会面
</
asp:ListItem
>
<
asp:ListItem
Value
="4"
>
了解行业动向
</
asp:ListItem
>
<
asp:ListItem
Value
="5"
>
观看观摩
</
asp:ListItem
>
<
asp:ListItem
Value
="6"
>
投资项目洽谈
</
asp:ListItem
>
<
asp:ListItem
Value
="7"
>
获取行业信息
</
asp:ListItem
>
<
asp:ListItem
Value
="8"
>
寻找供应商或买家
</
asp:ListItem
>
<
asp:ListItem
Value
="-1"
>
其他
</
asp:ListItem
>
</
asp:CheckBoxList
>
<
ShiXun:ListControlRequiredFieldValidator
ControlToValidate
="cbPurpose"
display
="Dynamic"
ErrorMessage
="至少选择一项!"
EnableClientScript
="true"
runat
="Server"
>
至少选择一项
</
ShiXun:ListControlRequiredFieldValidator
>
<
br
/>
<
asp:CheckBoxList
ID
="cbChannel"
runat
="server"
RepeatColumns
="3"
RepeatDirection
="Horizontal"
>
<
asp:ListItem
Value
="1"
>
报纸等平面媒体
</
asp:ListItem
>
<
asp:ListItem
Value
="2"
>
网络
</
asp:ListItem
>
<
asp:ListItem
Value
="3"
>
电视
</
asp:ListItem
>
<
asp:ListItem
Value
="4"
>
朋友介绍
</
asp:ListItem
>
<
asp:ListItem
Value
="5"
>
客户推荐
</
asp:ListItem
>
<
asp:ListItem
Value
="-1"
>
其他
</
asp:ListItem
>
</
asp:CheckBoxList
>
<
ShiXun:ListControlRequiredFieldValidator
ControlToValidate
="cbChannel"
display
="Dynamic"
ErrorMessage
="至少选择一项!"
EnableClientScript
="true"
runat
="Server"
>
至少选择一项
</
ShiXun:ListControlRequiredFieldValidator
>
<
asp:TextBox
ID
="txtSourceChannel"
runat
="Server"
CssClass
="input2"
Width
="400px"
></
asp:TextBox
>
<
br
/>
<
br
/>
<
br
/>
<
asp:Button
ID
="Button1"
runat
="server"
Text
="Button"
/>
</
form
>
</
body
>
</
html
>
本文提供了一个Page页面中多个CheckBoxList的客户端验证方法,包括基于.NET 1.1和.NET 2.0的不同实现方式,适用于确保用户至少选择一个选项。

被折叠的 条评论
为什么被折叠?



