一个Page页面多个CheckBoxList的客户端验证(.net1.1及2.0实现)

本文介绍了一个Page页面上多个CheckBoxList的客户端验证方法,分别针对.NET 1.1和.NET 2.0提供了自定义验证控件的实现细节。包括类文件和页面代码示例,展示了如何确保至少有一个选项被选中。
一,基于.net1.1
1、类文件
None.gif     
None.gif
using  System;
None.gif
using  System.Web.UI;
None.gif
using  System.Web;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Text;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
namespace  AgrExibition.Common.Framework.Util  ... {
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
欢迎与邀月交流,net技术与软件架构 2007/07/26#region downmoon 2007/07/26
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 一个Page页面多个CheckBoxList的客户端验证(.net1.1实现)(downmoon)
ExpandedSubBlockEnd.gif    
/// </summary>

ExpandedSubBlockEnd.gif    #endregion

InBlock.gif    
public class RadioButtonListRequiredFieldValidator : BaseValidator 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
...{
InBlock.gif
InBlock.gif        
protected override bool ControlPropertiesValid()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override bool EvaluateIsValid() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif            
return this.EvaluateIsChecked();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected bool EvaluateIsChecked() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif            ListControl _listcontrol 
= ((ListControl)this.FindControl(this.ControlToValidate)); 
InBlock.gif            
foreach( ListItem li in _listcontrol.Items ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
...{
InBlock.gif                
if (li.Selected) return true
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return false
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnPreRender( EventArgs e )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if ( this.EnableClientScript ) ...this.ClientScript(); }
InBlock.gif            
base.OnPreRender( e );
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected void ClientScript() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif            
this.Attributes["evaluationfunction"= "cb_vefify";
InBlock.gif
InBlock.gif            StringBuilder sb_Script 
= new StringBuilder();
InBlock.gif            sb_Script.Append( 
"<script language="javascript">" );
InBlock.gif            sb_Script.Append( 
" " );
InBlock.gif            sb_Script.Append( 
" " );
InBlock.gif            sb_Script.Append( 
"function cb_vefify(val) {" );
InBlock.gif            sb_Script.Append( 
" " );
InBlock.gif            sb_Script.Append(
"var val =document.getElementById(val.controltovalidate);");
InBlock.gif            sb_Script.Append( 
" " );
InBlock.gif            sb_Script.Append( 
"var col = val.all;" );
InBlock.gif            sb_Script.Append( 
" " );
InBlock.gif            sb_Script.Append( 
"if ( col != null ) {" );
InBlock.gif            sb_Script.Append( 
" " );
InBlock.gif            sb_Script.Append( 
"for ( i = 0; i < col.length; i++ ) {" );
InBlock.gif            sb_Script.Append( 
" " );
InBlock.gif            sb_Script.Append( 
"if (col.item(i).tagName.toUpperCase() == "INPUT") {" );
InBlock.gif            sb_Script.Append( 
" " );
InBlock.gif            sb_Script.Append( 
"if ( col.item(i).checked ) {" );
InBlock.gif            sb_Script.Append( 
" " );
InBlock.gif            sb_Script.Append( 
" " );
InBlock.gif            sb_Script.Append( 
"return true;" );
InBlock.gif            sb_Script.Append( 
" " );
InBlock.gif            sb_Script.Append( 
"}" );
InBlock.gif            sb_Script.Append( 
" " );
InBlock.gif            sb_Script.Append( 
"}" );
InBlock.gif            sb_Script.Append( 
" " );
InBlock.gif            sb_Script.Append( 
"}" );
InBlock.gif            sb_Script.Append( 
" " );
InBlock.gif            sb_Script.Append( 
" " );
InBlock.gif            sb_Script.Append( 
" " );
InBlock.gif            sb_Script.Append( 
"return false;" );
InBlock.gif            sb_Script.Append( 
" " );
InBlock.gif            sb_Script.Append( 
"}" );
InBlock.gif            sb_Script.Append( 
" " );
InBlock.gif            sb_Script.Append( 
"}" );
InBlock.gif            sb_Script.Append( 
" " );
InBlock.gif            sb_Script.Append( 
"</script>" );
InBlock.gif            
if(!this.Page.IsClientScriptBlockRegistered("RBLScript"))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
...{
InBlock.gif                
this.Page.RegisterClientScriptBlock( "RBLScript", sb_Script.ToString() );
ExpandedSubBlockEnd.gif            }

InBlock.gif   
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

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


二、基于.net 2.0
1、类文件
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.ComponentModel;
None.gif
using  System.Text;
None.gif
using  System.Web;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
None.gif
namespace  ShiXun.Commoon.Web.Validor
ExpandedBlockStart.gifContractedBlock.gif
... {
InBlock.gif   
ContractedSubBlock.gifExpandedSubBlockStart.gif   
欢迎与邀月交流,net技术与软件架构 2007/07/26#region downmoon 2007/07/26
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 一个Page页面多个CheckBoxList的客户端验证(.net2.0实现)(downmoon)
ExpandedSubBlockEnd.gif    
/// </summary>

ExpandedSubBlockEnd.gif    #endregion

InBlock.gif   
InBlock.gif    [DefaultProperty(
"ErrorMessage"), ToolboxData("<{0}:ListControlRequiredFieldValidator runat=server></{0}:ListControlRequiredFieldValidator>")]
InBlock.gif    
public class ListControlRequiredFieldValidator : BaseValidator
ExpandedSubBlockStart.gifContractedSubBlock.gif    
...{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Validator Requirement
InBlock.gif        
/// </summary>
InBlock.gif
ExpandedSubBlockEnd.gif        
/// <returns>True if dependencies are valid.</returns>

InBlock.gif        protected override bool ControlPropertiesValid()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif            Control controlToValidate 
= FindControl(ControlToValidate) as ListControl;
InBlock.gif            
return (controlToValidate != null);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Validator Requirement
InBlock.gif        
/// </summary>
InBlock.gif        
/// <returns>true if ControlToValidate 
ExpandedSubBlockEnd.gif        
/// has one item or more selected</returns>

InBlock.gif
InBlock.gif        
protected override bool EvaluateIsValid()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif            
return this.EvaluateIsChecked();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Return true if an item in the list is selected.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <returns>true if ControlToValidate 
ExpandedSubBlockEnd.gif        
///      has one item or more selected</returns>

InBlock.gif        protected bool EvaluateIsChecked()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif            ListControl listToValidate 
= ((ListControl)this.FindControl(this.ControlToValidate));
InBlock.gif
InBlock.gif            
foreach (ListItem li in listToValidate.Items)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
...{
InBlock.gif                
if (li.Selected == true)
InBlock.gif                    
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif
InBlock.gif        
/// Pre Render
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="e"></param >

InBlock.gif        protected override void OnPreRender(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif            System.Web.HttpContext.Current.Trace.Write(
"Override OnPreRender");
InBlock.gif            
if (this.DetermineRenderUplevel() && this.EnableClientScript)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
...{
InBlock.gif                Page.ClientScript.RegisterExpandoAttribute(
this.ClientID, "evaluationfunction""ListItemVerify");
InBlock.gif                Page.ClientScript.RegisterExpandoAttribute(
this.ClientID, "minimumNumberOfSelectedCheckBoxes""1");
InBlock.gif
InBlock.gif                
//System.Web.UI.Page.(this.ClientID, "evaluationfunction", "ListItemVerify");
InBlock.gif                
//Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "minimumNumberOfSelectedCheckBoxes", "1"); 
InBlock.gif
InBlock.gif
InBlock.gif                
//TODO: imporove to allow variable number.
InBlock.gif
                this.RegisterClientScript();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
...{
InBlock.gif                
this.Attributes.Remove("evaluationfunction");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.OnPreRender(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Register the client script.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif
InBlock.gif        
protected void RegisterClientScript()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif            
string script = @"
InBlock.gif
InBlock.gif            <script language=""javascript"">
InBlock.gif            function ListItemVerify(val) 
InBlock.gif            {
InBlock.gif                var control = document.getElementById(val.controltovalidate);
InBlock.gif                var minimumNumberOfSelectedCheckBoxes = parseInt(val.minimumNumberOfSelectedCheckBoxes);
InBlock.gif                var selectedItemCount = 0;
InBlock.gif                var liIndex = 0;
InBlock.gif                var currentListItem = document.getElementById(control.id + '_' + liIndex.toString());
InBlock.gif                while (currentListItem != null)
InBlock.gif                {
InBlock.gif                    if (currentListItem.checked) selectedItemCount++;
InBlock.gif                    liIndex++;
InBlock.gif                    currentListItem = document.getElementById(control.id + '_' + liIndex.toString());
InBlock.gif                }
InBlock.gif                return selectedItemCount >= minimumNumberOfSelectedCheckBoxes;
InBlock.gif            }
InBlock.gif            </script>
InBlock.gif            
";
InBlock.gif
InBlock.gif            
this.Page.ClientScript.RegisterClientScriptBlock(typeof(ListControlRequiredFieldValidator), "ListRequiredValidator_Script", script);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

2、页面

ExpandedBlockStart.gif ContractedBlock.gif <% ... @ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default"  %>
ExpandedBlockStart.gifContractedBlock.gif
<% ... @ Register TagPrefix="ShiXun" NameSpace="ShiXun.Commoon.Web.Validor" Assembly="ShiXun.Commoon.Web.Validor"  %>
None.gif
None.gif
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
None.gif
None.gif
< html  xmlns ="http://www.w3.org/1999/xhtml"   >
None.gif
< head  runat ="server" >
None.gif    
< title > 无标题页 </ title >
None.gif
</ head >
None.gif
< body >
None.gif    
< form  id ="form1"  runat ="server" >
None.gif   
< asp:CheckBoxList  id ="cbB_Property"  runat ="server"  RepeatDirection ="Horizontal"  RepeatColumns ="3" >
None.gif                
< asp:ListItem  Value ="1" > 投资商 </ asp:ListItem >
None.gif                
< asp:ListItem  Value ="2" > 投资促进机构 </ asp:ListItem >
None.gif                
< asp:ListItem  Value ="3" > 代理商 </ asp:ListItem >
None.gif                
< asp:ListItem  Value ="4" > 科研及信息单位协会 </ asp:ListItem >
None.gif                
< asp:ListItem  Value ="5" > 零售商 </ asp:ListItem >
None.gif                
< asp:ListItem  Value ="6" > 批发商 </ asp:ListItem >
None.gif                
< asp:ListItem  Value ="7" > 仓储业 </ asp:ListItem >
None.gif                
< asp:ListItem  Value ="8" > 跨国公司 </ asp:ListItem >
None.gif                
< asp:ListItem  Value ="9" > 进出口商 </ asp:ListItem >
None.gif                
< asp:ListItem  Value ="10" > 生产商 </ asp:ListItem >
None.gif                
< asp:ListItem  Value ="11" > 超市 </ asp:ListItem >
None.gif                
< asp:ListItem  Value ="12" > 酒店 </ asp:ListItem >
None.gif                
< asp:ListItem  Value ="13" > 宾馆及后勤服务 </ asp:ListItem >
None.gif                
< asp:ListItem  Value ="-1" > 其他 </ asp:ListItem >
None.gif            
</ asp:CheckBoxList >
None.gif            
< ShiXun:ListControlRequiredFieldValidator
None.gif    
ControlToValidate ="cbB_Property"  
None.gif    display
="Dynamic"  
None.gif    ErrorMessage
="至少选择一项!"  
None.gif    EnableClientScript
="true"  
None.gif    runat
="Server" >
None.gif    至少选择一项
None.gif
</ ShiXun:ListControlRequiredFieldValidator >
None.gif        
< br  />
None.gif        
< br  />
None.gif        
< asp:CheckBoxList  ID ="cbPurpose"  runat ="server"  RepeatColumns ="3"  RepeatDirection ="Horizontal" >
None.gif            
< asp:ListItem  Value ="1" > 采购产品 </ asp:ListItem >
None.gif            
< asp:ListItem  Value ="2" > 搜集市场信息 </ asp:ListItem >
None.gif            
< asp:ListItem  Value ="3" > 与业务伙伴会面 </ asp:ListItem >
None.gif            
< asp:ListItem  Value ="4" > 了解行业动向 </ asp:ListItem >
None.gif            
< asp:ListItem  Value ="5" > 观看观摩 </ asp:ListItem >
None.gif            
< asp:ListItem  Value ="6" > 投资项目洽谈 </ asp:ListItem >
None.gif            
< asp:ListItem  Value ="7" > 获取行业信息 </ asp:ListItem >
None.gif            
< asp:ListItem  Value ="8" > 寻找供应商或买家 </ asp:ListItem >
None.gif            
< asp:ListItem  Value ="-1" > 其他 </ asp:ListItem >
None.gif        
</ asp:CheckBoxList >
None.gif        
< ShiXun:ListControlRequiredFieldValidator
None.gif    
ControlToValidate ="cbPurpose"  
None.gif    display
="Dynamic"  
None.gif    ErrorMessage
="至少选择一项!"  
None.gif    EnableClientScript
="true"  
None.gif    runat
="Server" >
None.gif    至少选择一项
None.gif
</ ShiXun:ListControlRequiredFieldValidator >
None.gif        
< br  />
None.gif        
< asp:CheckBoxList  ID ="cbChannel"  runat ="server"  RepeatColumns ="3"  RepeatDirection ="Horizontal" >
None.gif            
< asp:ListItem  Value ="1" > 报纸等平面媒体 </ asp:ListItem >
None.gif            
< asp:ListItem  Value ="2" > 网络 </ asp:ListItem >
None.gif            
< asp:ListItem  Value ="3" > 电视 </ asp:ListItem >
None.gif            
< asp:ListItem  Value ="4" > 朋友介绍 </ asp:ListItem >
None.gif            
< asp:ListItem  Value ="5" > 客户推荐 </ asp:ListItem >
None.gif            
< asp:ListItem  Value ="-1" > 其他 </ asp:ListItem >
None.gif        
</ asp:CheckBoxList >
None.gif        
< ShiXun:ListControlRequiredFieldValidator
None.gif    
ControlToValidate ="cbChannel"  
None.gif    display
="Dynamic"  
None.gif    ErrorMessage
="至少选择一项!"  
None.gif    EnableClientScript
="true"  
None.gif    runat
="Server" >
None.gif    至少选择一项
None.gif
</ ShiXun:ListControlRequiredFieldValidator >
None.gif        
< asp:TextBox  ID ="txtSourceChannel"  runat ="Server"  CssClass ="input2"  Width ="400px" ></ asp:TextBox >
None.gif        
< br  />
None.gif        
< br  />
None.gif        
< br  />
None.gif        
< asp:Button  ID ="Button1"  runat ="server"  Text ="Button"   />
None.gif    
</ form >
None.gif
</ body >
None.gif
</ html >
None.gif

转载于:https://www.cnblogs.com/downmoon/archive/2007/12/29/1020089.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值