3

問題:如何正確地擴展自定義控件以使用RequiredFieldValidator?我讀過this,但試圖實施它並沒有解決我的問題。 這是我想出迄今:'RequiredFieldValidator'引用的自定義控件無法驗證

<%@ Register src="DynamicGenerator/Controls/ReferenceControl.ascx" tagname="ReferenceControl" tagprefix="uc1" %> 
    <uc1:ReferenceControl ID="SelectAgreement" runat="server" /> 
    <asp:RequiredFieldValidator ID="RequiredFieldValidator" ControlToValidate="SelectAgreement" runat="server" ErrorMessage="Select an agreement!"></asp:RequiredFieldValidator> 

後面的代碼:

[ValidationProperty("ConceptDefinitionId")] 
public partial class ReferenceSelector : System.Web.UI.UserControl, IReference 
{ 
    //lots of other stuff 
public string ConceptDefinitionId 
     { 
      get { return ReferenceControl.ConceptDefinitionId ?? ""; } 
      set { ReferenceControl.ConceptDefinitionId = value; } 
     } 
} 

加載這個頁面給了我以下錯誤:

控制「SelectAgreement」通過引用'RequiredFieldValidator'的ControlToValidate屬性無法驗證。

堆棧跟蹤:

[HttpException (0x80004005): Control 'SelectAgreement' referenced by the ControlToValidate property of 'RequiredFieldValidator' cannot be validated.] 
    System.Web.UI.WebControls.BaseValidator.CheckControlValidationProperty(String name, String propertyName) +8739685 
    System.Web.UI.WebControls.BaseValidator.ControlPropertiesValid() +40 
    System.Web.UI.WebControls.BaseValidator.get_PropertiesValid() +21 
    System.Web.UI.WebControls.BaseValidator.OnPreRender(EventArgs e) +27 
    System.Web.UI.Control.PreRenderRecursiveInternal() +80 
    System.Web.UI.Control.PreRenderRecursiveInternal() +171 
    System.Web.UI.Control.PreRenderRecursiveInternal() +171 
    System.Web.UI.Control.PreRenderRecursiveInternal() +171 
    System.Web.UI.Control.PreRenderRecursiveInternal() +171 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842 
+0

你的代碼看起來我的權利。我得到這個問題有同樣的問題,除了我錯過了'ValidationPropertyAttribute'。只要我添加了_(我的屬性是隻讀的並返回'int',作爲比較)_它按預期工作。 – JMD 2016-04-12 20:11:14

回答

3

你將需要把用戶控件內RequiredFieldValidator和驗證一個特定的控制就像一個TextBoxDropDown,因爲驗證只允許在相同的命名容器,並在你的情況下,用戶控件是不同的容器,所以這是行不通的。

另一種方法可以使用ValidationPropertyAttribute

+1

我已經在使用ValidationPropertyAttribute(請參閱代碼隱藏),但我想我以錯誤的方式使用它? – 2011-05-12 11:36:16

+0

嘗試更改'EnableClientScript'屬性 – V4Vendetta 2011-05-12 11:37:58

+0

試圖在RequiredFieldEditor上將它設置爲true和false,但沒有結果。 – 2011-05-12 11:46:46