C# 由';引用的自定义控件;RequiredFieldValidator';无法验证
问题:如何正确扩展自定义控件以使用RequiredFieldValidator?我读过,但试图实现它并没有解决我的问题。 这就是我到目前为止的想法:C# 由';引用的自定义控件;RequiredFieldValidator';无法验证,c#,asp.net,custom-controls,requiredfieldvalidator,validation-controls,C#,Asp.net,Custom Controls,Requiredfieldvalidator,Validation Controls,问题:如何正确扩展自定义控件以使用RequiredFieldValidator?我读过,但试图实现它并没有解决我的问题。 这就是我到目前为止的想法: <%@ Register src="DynamicGenerator/Controls/ReferenceControl.ascx" tagname="ReferenceControl" tagprefix="uc1" %> <uc1:ReferenceControl ID="SelectAgreement" runat
<%@ 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>
加载此页面时出现以下错误:
无法验证“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内部,并验证特定控件,如文本框
或下拉列表
,因为验证器只允许位于同一命名容器中,并且在您的情况下,UserControl是不同的容器,因此这将不起作用
另一种方法是使用我已经在使用ValidationPropertyAttribute(参见代码隐藏),但是我想我用错了吗?试着在RequiredFieldEditor上更改
EnableClientScript
propertyTried设置,将其设置为true和false,但没有结果。如果你想验证自定义UserControl
的两个实例,你不能(很简单地)通过放置验证器来实现在UserControl内部。在这种情况下,最好的解决方案是使用ValidationPropertyAttribute
。您指定的自定义控件的任何属性都将通过.ToString()
传递给验证器。(我的属性是只读的int
)我觉得您的代码很正确。我问这个问题时遇到了同样的问题,只是缺少了ValidationPropertyAttribute
。一旦我添加了这个属性(我的属性是只读的,并且返回int
,以便比较),它就如预期的那样工作了。
[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