2

I want to get the ControlToValidate Property From Code Behind, here is my aspx.

<asp:CustomValidator runat="server" ID="custtxtTest" OnServerValidate="custtxtTest_ServerValidate" ControlToValidate="txtTest" ForeColor="Red" Text="*" />

In my Code behind I want to get the property "ControlToValidate", but this doesn't seem to be a valid property of source:

 protected void custtxtTest_ServerValidate(object source, ServerValidateEventArgs args)
    {
    string test = source.ControlToValidate;
    }

I checked Asp.Net custom validator: how to get the 'controlToValidate' property on ClientValidationFunction? but that is only for a client function, not in code behind.

Community
  • 1
  • 1
Joe Stellato
  • 558
  • 9
  • 31

1 Answers1

7

This should work:

var validator = (source as CustomValidator);
string controlToValidate = validator.ControlToValidate;            
TextBox txt = validator.NamingContainer.FindControl(controlToValidate) as TextBox;

Regards, Uros

Uroš Goljat
  • 1,806
  • 14
  • 15