Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Template Based Multiple Control Validator

, 25 Mar 2009
A templated server control that allows us to validate multiple controls with the same validation controls
ValidationAggregator_demo.zip
ValidationAggregator
SampleWebSite
App_Code
App_Data
Bin
ValidationAggregator.dll
ValidationAggregator.pdb
ValidationAggregator
bin
Properties
ValidationAggregator.4.0.resharper
ValidationAggregator.4.0.resharper.user
ValidationAggregator.gpState
ValidationAggregator.suo
ValidationAggregator_src.zip
bin
Release
ValidationAggregator.dll
Properties
using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ValidationAggregator;

namespace ValidationAggregator
{
	[
		AspNetHostingPermission(SecurityAction.Demand,
			Level = AspNetHostingPermissionLevel.Minimal),
		AspNetHostingPermission(SecurityAction.InheritanceDemand,
			Level = AspNetHostingPermissionLevel.Minimal),
		Designer(typeof(ValidatorDesigner)),
		DefaultProperty("ControlToValidate"),
		ToolboxData("<{0}:Validator runat=\"server\"></{0}:Validator>")
	]
	public class Validator : WebControl
	{
		#region Fields

		private string _controlToValidate;
		private string _templateId;
		private string _label;
		private string _validationGroup;

		#endregion

		#region Properties

		[
			Category("Behavior"),
			DefaultValue(""),
			Description("")
		]
		public string ControlToValidate
		{
			get { return _controlToValidate; }
			set { _controlToValidate = value; }
		}

		[
			Category("Behavior"),
			DefaultValue(""),
			Description("")
		]
		public string TemplateId
		{
			get { return _templateId; }
			set { _templateId = value; }
		}

		[
			Category("Behavior"),
			DefaultValue(""),
			Description("")
		]
		public string Label
		{
			get { return _label; }
			set { _label = value; }
		}

		[
			Category("Behavior"),
			DefaultValue(""),
			Description("Override Template Validation Group")
		]
		public string ValidationGroup
		{
			get { return _validationGroup; }
			set { _validationGroup = value; }
		}

		#endregion

		protected override void CreateChildControls()
		{
			Controls.Clear();
			// Search for ValidationTemplate.
			ValidationTemplate t = SearchValidationTemplate();

			if (t == null)
				throw new Exception(string.Format("Could not find ValidationTemplate \"{0}\" specified in control \"{1}\"",
				                                  _templateId, ID));

			Controls.Add(t.GetValidators(new ControlDesc(_controlToValidate, _label), _validationGroup ?? ""));
		}

		public override ControlCollection Controls
		{
			get
			{
				EnsureChildControls();
				return base.Controls;
			}
		}

		protected override void Render(HtmlTextWriter writer)
		{
			EnsureChildControls();
			Controls[0].RenderControl(writer);
		}

		private ValidationTemplate SearchValidationTemplate()
		{
			Control container = NamingContainer;
			ValidationTemplate vt;

			do
			{
				vt = (ValidationTemplate) container.FindControl(_templateId);
				container = container.NamingContainer;

			} while (vt == null && container != null);

			return vt;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Hisham Kedjour
Software Developer (Senior)
Algeria Algeria
Fascinated by good architectures, well thought classes and clean code. I work as a software developer since 2004.

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 25 Mar 2009
Article Copyright 2009 by Hisham Kedjour
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid