.net2.0 自定义CheckBoxList验证控件

转自:http://www.cnblogs.com/prolifes/articles/1385255.html

页面上有个CheckBoxList控件,用户必须选一个选项才能提交,现有的asp.net验证控件还不能验证CheckBoxList,怎么办?
1.新建CheckBoxListValidator类,继承BaseValidator

using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

using  System.Text;

namespace  CheckBoxListValidator
{
    
public   class  CheckBoxListValidator : BaseValidator
    {
        
protected   override   bool  ControlPropertiesValid()
        {
            
return   true ;
        }

        
protected   override   bool  EvaluateIsValid()
        {
            
return  EvaluateIsChecked();
        }

        
protected   bool  EvaluateIsChecked()
        {
            CheckBoxList _cbl 
=  ((CheckBoxList)FindControl(ControlToValidate));
            
foreach  (ListItem li  in  _cbl.Items)
            {
                
if  (li.Selected  ==   true )
                {
                    
return   true ;
                }
            }
            
return   false ;
        }

        
protected   override   void  OnPreRender(EventArgs e)
        {
            
if  (EnableClientScript)
            {
                ClientScript();
            }
            
base .OnPreRender(e);
        }

        
protected   void  ClientScript()
        {
            Attributes[
" evaluationfunction " =   " cb_vefify " ;

            StringBuilder sb_Script 
=   new  StringBuilder();
            sb_Script.Append(
" <script language=\ " javascript\ " > " );
            sb_Script.Append(
" \r " );
            sb_Script.Append(
" \r " );
            sb_Script.Append(
" function cb_vefify(val) { " );
            sb_Script.Append(
" \r " );
            sb_Script.Append(
" var val = document.all[document.all[\ "" );
            sb_Script.Append(this.ClientID);
            sb_Script.Append(
" \ " ].controltovalidate]; " );
            sb_Script.Append( " \r " );
            sb_Script.Append(
" var col = val.all; " );
            sb_Script.Append(
" \r " );
            sb_Script.Append(
" if ( col != null ) { " );
            sb_Script.Append(
" \r " );
            sb_Script.Append(
" for ( i = 0; i < col.length; i++ ) { " );
            sb_Script.Append(
" \r " );
            sb_Script.Append(
" if (col.item(i).tagName == \ " INPUT\ " ) { " );
            sb_Script.Append(
" \r " );
            sb_Script.Append(
" if ( col.item(i).checked ) { " );
            sb_Script.Append(
" \r " );
            sb_Script.Append(
" \r " );
            sb_Script.Append(
" return true; " );
            sb_Script.Append(
" \r " );
            sb_Script.Append(
" } " );
            sb_Script.Append(
" \r " );
            sb_Script.Append(
" } " );
            sb_Script.Append(
" \r " );
            sb_Script.Append(
" } " );
            sb_Script.Append(
" \r " );
            sb_Script.Append(
" \r " );
            sb_Script.Append(
" \r " );
            sb_Script.Append(
" return false; " );
            sb_Script.Append(
" \r " );
            sb_Script.Append(
" } " );
            sb_Script.Append(
" \r " );
            sb_Script.Append(
" } " );
            sb_Script.Append(
" \r " );
            sb_Script.Append(
" </script> " );

            Page.ClientScript.RegisterClientScriptBlock(GetType(), 
" RBLScript " , sb_Script.ToString(),  false );
        }
    }


}



2.将CheckBoxListValidator.cs拷至C盘下编译生成dll

csc  / out :C:\CheckBoxListValidator.dll  / target:library C:\CheckBoxListValidator.cs


3.将CheckBoxListValidator.dll添加到工具箱,拖放至default.aspx页面

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

<% @ Register Assembly = " CheckBoxListValidator "  Namespace = " CheckBoxListValidator "  TagPrefix = " cc2 "   %>


<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//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 " >
    
< div >
        
< asp:CheckBoxList ID = " CheckBoxList1 "  runat = " server " >
        
< asp:ListItem > 1 </ asp:ListItem >
            
< asp:ListItem > 2 </ asp:ListItem >
            
< asp:ListItem > 3 </ asp:ListItem >
            
< asp:ListItem > 4 </ asp:ListItem >
            
< asp:ListItem > 5 </ asp:ListItem >
            
< asp:ListItem > 6 </ asp:ListItem >
            
< asp:ListItem > 7 </ asp:ListItem >
        
</ asp:CheckBoxList >
        
< cc2:CheckBoxListValidator ControlToValidate = " CheckBoxList1 "
            ID
= " CheckBoxListValidator1 "  runat = " server " > 必选项 </ cc2:CheckBoxListValidator >
        
< asp:Button ID = " Button1 "  runat = " server "  Text = " Button "   /></ div >
    
</ form >
</ body >
</ html >


附:编译好的DLL下载 点击下载

你可能感兴趣的:(checkbox)