问题:如何正确扩展自定义控件以使用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
答案 0 :(得分:3)
您需要将RequiredFieldValidator
置于UserControl中并验证特定控件,如TextBox
或DropDown
,因为验证程序仅允许在同一命名容器中,在您的情况下UserControl是一个不同的容器,所以这不起作用。
另一种方法是使用ValidationPropertyAttribute