Creating A Designer Enabled Custom Validator Control
[DEMO]
By: Keith A Wells & King S Wells Jr.
This is a two-part article. The first part of this article will discuss how to create server-side validation for your custom control and the second part will conclude with a discussion on how to complete your control by adding client-side validation to your control.
PART I: Server-Side Validation
The new Microsoft .NET Framework has made the development of web based applications more structured and maintainable. I have also discovered that there are many features that make web development easier but there are also features that make the development more difficult than in classical ASP. The use of object-oriented methodologies instead of the old spaghetti code provided in classical ASP is one such feature that makes development better when developing web applications using ASP.NET. Another feature in ASP.NET that makes development easier is the introduction of five new validation controls. The use of these five controls will cut down development time significantly. I especially like the RegularExpressionValidator. This validator is very flexible and meets many of the needs of the modern developer. The other four validation controls are the RequiredFieldValidator, CompareValidator, RangeValidator and CustomValidator. All of these controls play a significant role in cutting down on development time. But, as is often the case, we find that a development platform cannot always meet our needs or the needs of our customer. In such cases, we are required to develop our own solution. Because the development of a customized solution is often the case, I will show you how to develop a custom validator to address this need.
Overview
I have chosen to create a validator called the DualValidator. The concept for the DualValidator came from a validation requirement given to my brother by one of his customers. In order to meet the deadline, we worked together on this particular requirement.
This customer required that either one of two controls may contain a value but not both. It was also required that the validator would only perform this test when a third control which I call the PrimaryControl contained a value. To summarize, this validator validated two controls based on the value contained in a third control.
In order to create a reusable custom validator, the control needs to be able to validate any control given to it. It is also a good idea to give the control the ability to be added to the designer’s toolbox. One such designer is Visual Studio.NET.
Defining The TagPrefix
The TagPrefix is an assembly level attribute that allows you to automatically create a Register directive in your aspx page.
[assembly:TagPrefix("Southpoint.Framework.Validator", "val")]
An assembly-level attribute that allows you to define an alias for your control’s tag prefix.
E.g.
<%@ Register TagPrefix="val" Namespace="Southpoint.Framework.Validator" Assembly="DualValidator" %>
<meta content="True" > |
[assembly:TagPrefix("Southpoint.Validator", "val")]
namespace Southpoint.Framework.Validator
{ /// <summary>
/// Summary description for DualValidator.
/// </summary> |
Defining Toolbox Default Values
If you want your control to have a meaningful tag when you drag your control onto a form, then you will need to define this tag by using the ToolboxAttribute attribute class. All attribute classes end with the word Attribute. But, when you are declaring your attribute, you may leave the word Attribute off. See the example below.
ToolboxData("<{0}:DualValidator runat=server></{0}:DualValidator>")
Or
ToolboxDataAttribute("<{0}:DualValidator runat=server></{0}:DualValidator>")
The ToolBoxDataAttribute defines the default tag that is generated when the control is dragged onto a web form.
E.g <val:Dualvalidator runat=server></val:DualValidator> |