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): 26503/ 232

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

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 1 and 4 and type the answer here:

User Comments

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 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






Ads Powered by Lake Quincy Media
Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 8/21/2008 6:25:12 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search