CheckBoxListValidator.cs :  » Content-Management-Systems-CMS » mojoPortal » SurveyFeature » UI » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » mojoPortal 
mojoPortal » SurveyFeature » UI » CheckBoxListValidator.cs
using System.Web;
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.IO;
using System.Web.Configuration;
using System.Globalization;

//Taken from Scott Mitchell article http://scottonwriting.net/sowblog/posts/10039.aspx

namespace SurveyFeature.UI{
    public class CheckBoxListValidator : BaseValidator
    {
        [Description("The minimum number of CheckBoxes that must be checked to be considered valid.")]
        public int MinimumNumberOfSelectedCheckBoxes
        {
            get
            {
                object o = ViewState["MinimumNumberOfSelectedCheckBoxes"];
                if (o == null)
                    return 1;
                else
                    return (int)o;
            }
            set
            {
                ViewState["MinimumNumberOfSelectedCheckBoxes"] = value;
            }
        }

        private CheckBoxList _ctrlToValidate;

        protected CheckBoxList CheckBoxListToValidate
        {
            get
            {
                if (_ctrlToValidate == null)
                    _ctrlToValidate = FindControl(this.ControlToValidate) as CheckBoxList;

                return _ctrlToValidate;
            }
        }

        protected override bool ControlPropertiesValid()
        {
            // Make sure ControlToValidate is set
            if (this.ControlToValidate.Length == 0)
                throw new HttpException(string.Format(CultureInfo.CurrentCulture, "The ControlToValidate property of '{0}' cannot be blank.", this.ID));

            // Ensure that the control being validated is a CheckBoxList
            if (CheckBoxListToValidate == null)
                throw new HttpException(string.Format(CultureInfo.CurrentCulture, "The CheckBoxListValidator can only validate controls of type CheckBoxList."));

            // ... and that it has at least MinimumNumberOfSelectedCheckBoxes ListItems
            if (CheckBoxListToValidate.Items.Count < MinimumNumberOfSelectedCheckBoxes)
                throw new HttpException(string.Format(CultureInfo.CurrentCulture, "MinimumNumberOfSelectedCheckBoxes must be set to a value greater than or equal to the number of ListItems; MinimumNumberOfSelectedCheckBoxes is set to {0}, but there are only {1} ListItems in '{2}'", MinimumNumberOfSelectedCheckBoxes, CheckBoxListToValidate.Items.Count, CheckBoxListToValidate.ID));

            return true;    // if we reach here, everything checks out
        }

        protected override bool EvaluateIsValid()
        {
            // Make sure that the CheckBoxList has at least MinimumNumberOfSelectedCheckBoxes ListItems selected
            int selectedItemCount = 0;
            foreach (ListItem cb in CheckBoxListToValidate.Items)
                if (cb.Selected) selectedItemCount++;

            return selectedItemCount >= MinimumNumberOfSelectedCheckBoxes;
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);

            // Add the client-side code (if needed)
            if (this.RenderUplevel)
            {
                // Indicate the mustBeChecked value and the client-side function to used for evaluation
                // Use AddAttribute if Helpers.EnableLegacyRendering is true; otherwise, use expando attributes
                if (this.EnableLegacyRendering())
                {
                    writer.AddAttribute("evaluationfunction", "CheckBoxListValidatorEvaluateIsValid", false);
                    writer.AddAttribute("minimumNumberOfSelectedCheckBoxes", MinimumNumberOfSelectedCheckBoxes.ToString(CultureInfo.CurrentCulture), false);
                }
                else
                {
                    this.Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "evaluationfunction", "CheckBoxListValidatorEvaluateIsValid", false);
                    this.Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "minimumNumberOfSelectedCheckBoxes", MinimumNumberOfSelectedCheckBoxes.ToString(CultureInfo.CurrentCulture), false);
                }
            }
        }

        private bool EnableLegacyRendering()
        {
            return false;
           //bool result;

            //try
            //{
            //    string webConfigFile = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, "web.config");
            //    XmlTextReader webConfigReader = new XmlTextReader(new StreamReader(webConfigFile));
            //    result = ((webConfigReader.ReadToFollowing("xhtmlConformance")) && (webConfigReader.GetAttribute("mode") == "Legacy"));
            //    webConfigReader.Close();
            //}
            //catch(ArgumentOutOfRangeException)
            //{
            //    result = false;
            //}
            //return result;
        }

        public void RegisterScripts()
        {
            string folderPath = WebConfigurationManager.AppSettings.Get("CSSFriendly-JavaScript-Path");
            if (String.IsNullOrEmpty(folderPath))
            {
                folderPath = "~/ClientScript";
            }
            string filePath = folderPath.EndsWith("/") ? folderPath + "skmValidators.js" : folderPath + "/skmValidators.js";
            this.Page.ClientScript.RegisterClientScriptInclude(GetType(), GetType().ToString(), this.Page.ResolveUrl(filePath));
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            RegisterScripts();

            // Register the client-side function using WebResource.axd (if needed)
            // see: http://aspnet.4guysfromrolla.com/articles/080906-1.aspx
            //if (this.RenderUplevel && this.Page != null && !this.Page.ClientScript.IsClientScriptIncludeRegistered(this.GetType(), "skmValidators"))
            //    this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "skmValidators", this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "skmValidators.skmValidators.js"));
        }

    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.