“RequiredFieldValidator”引用的自定义控件;无法验证

发布于 2024-11-06 22:13:40 字数 2009 浏览 6 评论 0原文

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

Question: How do I correctly extend a custom control to work with the RequiredFieldValidator? I've read this, but trying to implement it didn't solve the problem for me.
This is what I've come up with so far:

 <%@ 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>

Code behind:

[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; }
        }
}

Loading this page gives me the following error:

Control 'SelectAgreement' referenced by the ControlToValidate property of 'RequiredFieldValidator' cannot be validated.

stacktrace:

[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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居人 2024-11-13 22:13:40

您需要将 RequiredFieldValidator 放置在 UserControl 中并验证特定控件,例如 TextBoxDropDown,因为验证器仅允许在同一控件中使用。命名容器,在您的情况下, UserControl 是一个不同的容器,因此这不起作用。

另一种方法可以是使用 验证属性属性

You will need to place the RequiredFieldValidator inside the UserControl and validate a specific control like a TextBox or DropDown, since validators are allowed only in the same naming container and in your case the UserControl is a different container so this won't work.

Another way could be using ValidationPropertyAttribute

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文