Nugs Nugs - 1 year ago 103
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?


Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download