Creating A Designer Enabled Custom Validator Control Pt. I
page 1 of 5
Published: 10 Feb 2004
Unedited - Community Contributed
Abstract
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.
by King & Keith Wells
Feedback
Average Rating: 
Views (Total / Last 10 Days): 45015/ 70

Server-Side Validation

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>

    


View Entire Article

User Comments

Title: Kevin-Rookie   
Name: JamesT
Date: 1/8/2009 1:51:13 PM
Comment:
Kevin, you are a rookie. A custom validation control is needed when the current controls do not meet your criteria.
Some validation rules are too complex and require custom handling.
Title: very very thanks   
Name: s/w eng. Rituraj pandey
Date: 11/20/2008 1:25:25 AM
Comment:
hi, i am very-2 thak full to you because this is a greated need to me and this is a great help to me so thank you.
Title: No   
Name: Kevin
Date: 6/27/2008 7:26:45 AM
Comment:
No, this is not helpfull. Why does every one want to increase the file size by creating custom validations as a script. Use the syntax when you assign the validation to a text box or field
Title: custom control   
Name: Rahul
Date: 9/5/2007 7:00:00 AM
Comment:
thnx give the best examples.so that we can learn more info abt custom validation.
Regards,
Rahul
Title: Code Download Available?   
Name: Edward
Date: 7/2/2007 12:35:39 PM
Comment:
Is the complete code download available?

Thanks,
Ed
Title: Microsoft Validation for Custom Controls?   
Name: Alfred
Date: 11/4/2005 12:13:57 PM
Comment:
Thank you for your article, I would like to ask you how to create user control that standard validation control can recognise it, that is UserControl name will appear in ControlToValidate list?
Thank you.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2014 ASPAlliance.com  |  Page Processed at 10/1/2014 4:17:42 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search