3

问题:如何正确扩展自定义控件以使用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
4

1 回答 1

3

您需要将其放置RequiredFieldValidator在 UserControl 内部并验证一个特定的控件,例如 a TextBoxor DropDown,因为验证器只允许在同一个命名容器中,并且在您的情况下 UserControl 是一个不同的容器,所以这不起作用。

另一种方法可能是使用ValidationPropertyAttribute

于 2011-05-12T11:31:24.657 回答