Creating A Designer Enabled Custom Validator Control Pt. I
page 5 of 5
by King & Keith Wells
Feedback
Average Rating: 
Views (Total / Last 10 Days): 24473/ 28

Server Side Validation Page 5
 Naming Container – a new namespace is created that insures that the control Ids are unique within an application. Every server control has a NamingContainer property. You can use this property to locate a control within a page. A new instance of the Page class is the default NamingContainer for a newly requested page.  

Because my DualValidator class has defined three new properties, there must be a way to locate the controls associated with these properties and acquire access to the values of these controls. The NamingContainer allows me to do so by searching its namespace using the FindControl() method.

Our requirement only required that we validate two dropdownlist objects, but with a few more lines of code, you can enhance this code to dynamically determine which type of control you need to cast your object into and access its values appropriately.  

(partial code)

TextBox      PrimaryControl = ((TextBox)NamingContainer.FindControl(primarycontrol));

 DropDownList ControlValue1 = ((DropDownList)NamingContainer.FindControl(control1));

 DropDownList ControlValue2 = ((DropDownList)NamingContainer.FindControl(control2));

FirstControl_Value =  ControlValue1.SelectedItem.Value.ToString();
SecondControl_Value =   

                         ControlValue2.SelectedItem.Value.ToString();  

                   if (PrimaryControl.Text.Trim() != String.Empty)

                      {
                                    if (FirstControl_Value.Trim() == String.Empty &&                                      SecondControl_Value.Trim() == String.Empty)

                                      {
                                                return false;

                                      }                                      

                                    else if (FirstControl_Value.Trim() != String.Empty &&                                             SecondControl_Value.Trim() != String.Empty)

                                      {
                                                return false;

                                      }  

Conclusion

This article showed us how to create a custom validator control that can be used in any .NET visual IDE designer.  Every web developer needs to know how to provide his web application with validation capabilities. Although Microsoft has assisted us greatly in the area of validation, a professional developer cannot escape the need to develop his own custom validator. I hope this series of articles will assist you in acquiring this ability.

In the next article, King is going to show you how to give your validator the ability to validate using client-side validation.  

Copyright: 2003 Keith A Wells and King S Wells Jr.


View Entire Article

User Comments

Title: Kevin-Rookie   
Name: JamesT
Date: 2009-01-08 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: 2008-11-20 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: 2008-06-27 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: 2007-09-05 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: 2007-07-02 12:35:39 PM
Comment:
Is the complete code download available?

Thanks,
Ed
Title: Microsoft Validation for Custom Controls?   
Name: Alfred
Date: 2005-11-04 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-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 1:46:29 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search