“RequiredFieldValidator”引用的自定义控件;无法验证
问题:如何正确扩展自定义控件以与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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将
RequiredFieldValidator
放置在 UserControl 中并验证特定控件,例如TextBox
或DropDown
,因为验证器仅允许在同一控件中使用。命名容器,在您的情况下, UserControl 是一个不同的容器,因此这不起作用。另一种方法可以是使用 验证属性属性
You will need to place the
RequiredFieldValidator
inside the UserControl and validate a specific control like aTextBox
orDropDown
, 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