Click here to Skip to main content
15,888,527 members
Articles / Web Development / ASP.NET

Template Based Multiple Control Validator

Rate me:
Please Sign up or sign in to vote.
4.82/5 (10 votes)
25 Mar 2009CPOL3 min read 39.1K   740   30  
A templated server control that allows us to validate multiple controls with the same validation controls
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)


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

Comments and Discussions