Asp.NET Tutorials
Home > 控件使用 > Required CheckBoxList Validator
Required CheckBoxList Validator

author:Mostafa Favaedi's

<H2>Introduction</H2>
<P>Please Extract RequiredCheckBoxListValidator.zip.
This code is Class
Library For Validate CheckBoxList and you can use this Class Library in any
Project.</P>
<P>Files :</P>
<P>1. RequiredCheckBoxListValidator.cs</P>
<DIV class=precollapse id=premain0 style="WIDTH: 100%"><IMG id=preimg0
style="CURSOR: hand" height=9 src="http://www.codeproject.com/images/minus.gif"
width=9 preid="0"> Collapse</DIV><PRE id=pre0 style="MARGIN-TOP: 0px">using System;
using System.IO;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RequiredCheckBoxListValidator
{
   
    public class RequiredCheckBoxListValidator : BaseValidator
    {
            
        protected override bool ControlPropertiesValid() { return true; }
        protected override bool EvaluateIsValid() { return this.EvaluateIsChecked(); }
        protected override void OnPreRender( EventArgs e )
        { if ( this.EnableClientScript ) { this.ClientScript(); } base.OnPreRender( e ); }
            
        protected bool EvaluateIsChecked()
        {
            ListControl _listcontrol = ((ListControl)this.FindControl(this.ControlToValidate));
            foreach( ListItem li in _listcontrol.Items )
            {
                if (li.Selected) return true; } return false;
        }
       
        protected void ClientScript()
        {
            this.Attributes["evaluationfunction"] = "RCBLV_Verify";
            if(!this.Page.IsClientScriptBlockRegistered("RCBLVScript"))
            {
                Assembly objAssembly = Assembly.GetExecutingAssembly();
                Stream objStream = objAssembly.GetManifestResourceStream("RequiredCheckBoxListValidator.RCBLV.js");
                StreamReader objReader = new StreamReader(objStream);
                string scriptBlock = objReader.ReadToEnd();
                this.Page.RegisterClientScriptBlock("RCBLVScript", scriptBlock);
            }
        }
    }
}

</pre>
<P>2. RCBLV.js (included in the project as an <EM>Embedded Resource</EM>) </P>
<P><IMG height=242 alt="Screenshot - EmbededResource.jpg"
src="Required_Validator/EmbededResource.jpg" width=251></P>
<H2>Using the code</H2>
<P>Please Extract UsingRequiredCheckBoxListValidator.zip.
This Code is Zero
Configuration to use Validation of CheckBoxList.</P>
<P>This Code have 3 Step to execute in other project:</P>
<P>1. Add DLL Reference in Project from
RequiredCheckBoxListValidator\bin\Debug\RequiredCheckBoxListValidator.dll
in
root of UsingRequiredCheckBoxListValidator Project</P>
<P><IMG height=191 alt="Screenshot - AddReference.jpg"
src="Required_Validator/AddReference.jpg" width=303> </P>
<P>2. Using page directive to utilize this control</P><PRE><%@ Register TagPrefix="webcontrols" Namespace="RequiredCheckBoxListValidator" Assembly="RequiredCheckBoxListValidator"%>
</PRE>
<P>3. Using RequiredCheckBoxListValidator</P><PRE><checkboxlist id="chklItems" runat="server">
    <listitem value="0">Item1</listitem>
        <listitem value="1">Item2</listitem>
        <listitem value="2">Item3</listitem>
<checkboxlist>
<requiredcheckboxlistvalidator id="RequiredCheckBoxListValidator1"
    runat="Server" text="Please Select Item" controltovalidate="chklItems" />
</PRE>

Add by : Huobazi (2007-6-04:11:25)