gpt4 book ai didi

c# - 无法验证 'RequiredFieldValidator' 引用的自定义控件

转载 作者:行者123 更新时间:2023-11-30 16:30:03 26 4
gpt4 key购买 nike

问题:如何正确扩展自定义控件以使用 RequiredFieldValidator?我读过 this ,但尝试实现它并没有解决我的问题。这是我到目前为止想出的:

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

最佳答案

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

另一种方法是使用 ValidationPropertyAttribute

关于c# - 无法验证 'RequiredFieldValidator' 引用的自定义控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5977139/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com