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

Server Side Validation Page 4

DISABLING THE CONTROLTOVALIDATE PROPERTY

 My DualValidator uses the values from two controls that are identified via the FirstControlToValidate property and the SecondControlToValidate property.  Since I have no need of the ControlToValidate property, I have hidden it from the property browser of Visual Studio.NET and have also raised an exception if a user tries to set the property’s value in the codebehind file.

       /// Disable ControlToValidate.       

       [Browsable(false)]
       [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]  
                public new string ControlToValidate
                {
                  get { return "";
                }
                  set { throw new NotSupportedException("Assign ControlToValidate on FirstControlToValidate and SecondControlToValidate instead.");
                        }

                }
  /// Since we are not using ControlToValidate, this should always return true.

                        protected override bool ControlPropertiesValid()

                        { 
                          
return true;                             

                        }

 

EvaluateIsValid THE HEART OF SERVER-SIDE VALIDATION

 Normally, the EvaluateIsValid method is used to determine if the value in the ControlToValidate property is valid. However, in my case, I disabled the ControlToValidate property and created three new properties for validating. This validator validates the values in two controls if and only if the value in the third control had a value. The property called PrimaryControl is used to determine whether we will do any validation on the FirstControlToValidate and SecondControlToValidate properties.  In order to keep this method clean and easily readable, I created a new method called ValidateDualControls that actually performs the validation. It receives three parameters. These parameters contain the value for the three properties listed above, the PrimaryControl, FirstControlToValidate and SecondControlToValidate properties that represent the controls that are involved in the validation routine.
           

            protected override bool EvaluateIsValid()
                        {                                  

return     

            ValidateDualControls(this.PrimaryControl,this.FirstControlToValidate, this.SecondControlToValidate);

                        }
                        protected bool ValidateDualControls(string primarycontrol, string      

                                               control1, string control2)
                        {
                                    string FirstControl_Value = "";
                                    string SecondControl_Value = "";

 

 


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-03-29 4:47:11 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search