Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get source control in OnServerValidate method

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

like image 447
Nugs Avatar asked Mar 08 '26 12:03

Nugs


2 Answers

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;
like image 102
user489998 Avatar answered Mar 10 '26 01:03

user489998


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
   }
}
like image 22
Garrett Vlieger Avatar answered Mar 10 '26 02:03

Garrett Vlieger