問題:如何正確地擴展自定義控件以使用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
你的代碼看起來我的權利。我得到這個問題有同樣的問題,除了我錯過了'ValidationPropertyAttribute'。只要我添加了_(我的屬性是隻讀的並返回'int',作爲比較)_它按預期工作。 – JMD 2016-04-12 20:11:14