Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Page.Validators but no UserControl.Validators

I have some forms composed of WebUserControls that have an Enabled property.  When a control is disabled I want to cycle thorough all its validators and disable them.  Page has a .Validators property that holds a collection of all the Validators on the page but WebUserControl doesn't.

Any reason for it being like this (I mean, am I doing The Right Thing with my Enabled property on my WebUserControl) or am I just unlucky here?

Thomas David Baker
Wednesday, December 17, 2003

The design decision is that validation is a page level activity, not a control level activity. It's the control's USER who decides what to validate and when.

Brad Wilson (dotnetguy.techieswithcats.com)
Wednesday, December 17, 2003

Sure, that's why my Controls have an Enabled property so that the USER of the Control can decide when to validate/show the control or not.  Only the code in my Enabled routine is fragile because I can't get at Control.Validators so I have to remember to add any validator I may add to the control to the routine as well.

Perhaps I'm using WebUserControls incorrectly?  I am using them to group several form elements and their validation partly because it's nicer to have the forms in manageable chunks but also because some of the chunks are used in multiple pages.

Thomas David Baker
Thursday, December 18, 2003

A validator keeps track of the control it validates through the ControlToValidate property.  As far as I know, there is no backwards reference from a control to a validator.  I believe I was able to accomplish something similar to what you're trying to do by cycling through Page.Validators and disabling any validators with ControlToValidate equal to the control I was disabling. 

SomeBody
Thursday, December 18, 2003

The alternative is to write a "meta-validator", and put that into Page.Validators. The job of this meta-validator is a clearing house for all the validators you want to control.

Brad Wilson (dotnetguy.techieswithcats.com)
Thursday, December 18, 2003

In the end I solved my problem in a fairly oblique way.  If a Control is not visible its Validators are not active.  So I just made everything invisible that I wasn't using.  Bit of a cop out but my code is much cleaner and more maintainable now!  Thanks for your help.

Thomas David Baker
Tuesday, January 13, 2004

*  Recent Topics

*  Fog Creek Home