2013-11-09 90 views
1

我想從代碼後面獲取ControlToValidate屬性,這裏是我的aspx。從代碼後面的CustomValidator獲取ControlToValidate屬性

<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?但這只是爲一個客戶端功能,不在代碼後面。

回答

5

這應該工作:

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

問候, 烏羅什

相關問題