问题:如何正确扩展自定义控件以使用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; }
}
}
加载此页面给我以下错误:
无法验证由“RequiredFieldValidator”的 ControlToValidate 属性引用的控件“SelectAgreement”。
堆栈跟踪:
[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