3

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
Soroush Hakami
  • 5,226
  • 15
  • 66
  • 99
  • Your code looks right to me. I got to this question having the same problem, except I was missing the `ValidationPropertyAttribute`. As soon as I added that _(my property is read-only and returns `int`, for comparison)_ it worked as expected. – JMD Apr 12 '16 at 20:11

1 Answers1

3

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

V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
  • 1
    I'm already using a ValidationPropertyAttribute (see the codebehind), but I suppose I am using it in the wrong way? – Soroush Hakami May 12 '11 at 11:36
  • Try changing the `EnableClientScript` property – V4Vendetta May 12 '11 at 11:37
  • Tried setting it both true and false on the RequiredFieldEditor with no result. – Soroush Hakami May 12 '11 at 11:46
  • If you want to validate two instances of your custom `UserControl` against each other you cannot (trivially) do so by putting the validator(s) inside the UserControl. The best solution in that case is to use `ValidationPropertyAttribute`. Whichever property of your custom control you specify is passed through to the validator with `.ToString()`. (My property is a read-only `int`.) – JMD Apr 12 '16 at 20:13