从代码隐藏中的CustomValidator获取ControlToValidate属性

时间:2013-11-09 14:45:19

标签: c# asp.net customvalidator

我想从Code Behind获取ControlToValidate属性,这是我的aspx。

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

在我的代码后面我想获取属性“ControlToValidate”,但这似乎不是源的有效属性:

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

我检查了Asp.Net custom validator: how to get the 'controlToValidate' property on ClientValidationFunction?,但这仅适用于客户端功能,而不是后面的代码。

1 个答案:

答案 0 :(得分:5)

这应该有效:

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

此致 乌罗什