Take the 2-minute tour ×

I have a CustomValidator control and within the OnServerValidate event i want to set properties of the control that is being validated.

You would think the validated control object would be available in the OnServerValidate event, but it is not. I then tried to do a FindControl but can't seem to get access to the ControlToValidate value.

Would someone please help me get the TextBox control being validated so that i can modify its properties from the OnServerValidate event?

Thanks, D

share|improve this question
If you know what control is being validated, why can't you access it directly? Is it nested in a databound control or something? –  James Johnson Nov 11 '11 at 17:49
I'm attempting to create a "universal" CustomValidator even that will be used by many different controls. –  Nugs Nov 11 '11 at 17:56
add comment

2 Answers

up vote 2 down vote accepted

Can you do something like this?

protected void CustomValidator1_ServerValidate (object source, ServerValidateEventArgs args)
   var validationControl = source as CustomValidator;

   var textBox = FindControl(validationControl.ControlToValidate) as TextBox;

   if (textBox != null)
      // Do something
share|improve this answer
Slightly different but you got it!! ContentPlaceHolder placeholder = Master.FindControl("placeholder_content") as ContentPlaceHolder; CustomValidator validationControl = source as CustomValidator; TextBox textBox = placeholder.FindControl(validationControl.ControlToValidate) as TextBox; if (textBox != null) { } –  Nugs Nov 11 '11 at 18:11
add comment

I tried the above answer but as my TextBox was in a ListView in a ContentPlaceHolder it all got a bit out of hand. I ended up using this which should work in all circumstances:

string textBoxName = ((CustomValidator)source).ControlToValidate;
var textBox = ((CustomValidator)source).Parent.FindControl(textBoxName) as TextBox;
share|improve this answer
add comment

Your Answer


