Nugs Nugs - 2 months ago 15
ASP.NET (C#) Question

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

Answer

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
   }
}