Question

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.

Was it helpful?

Solution

This should work:

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

Regards, Uros

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top