Custom text block with inline validator: server side validation fails

I have a class that looks like this:

public class TextField : TextBox
{
   public bool Required { get; set; }
   RequiredFieldValidator _validator;

   protected override void CreateChildControls()
   {
      base.CreateChildControls();


      _validator = new RequiredFieldValidator();
      _validator.ControlToValidate = this.ID;
      if(Required)
          Controls.Add(_validator);
   }

   public override void Render(HtmlTextWriter tw)
   {
      base.Render(tw);

      if(Required)
         _validator.RenderControl(tw);
   }
}

      

This works for a while in an internal application where javascript is always enabled. I recently noticed that an upstream JavaScript error can prevent validators from activating, so server side validation has to start ... right? is not it?

This way the Page.IsValid property always returns true (I even tried to explicitly call the Page.Validate () function).

After some digging, I found that the initator validator method should add the validator to the page, but due to the way I am building it up, I don't think this ever happens. So the client side validation works, but the server side validation is missing.

I've tried this:

protected override OnInit()
{
   base.OnInit();

   Page.Validators.Add(_validator); // <-- validator is null here
}

      

But of course the validator here is null (and sometimes not required, so it shouldn't be added) ... but OnInit () is really too early for me to make these decisions (the required property was not for example loaded from ViewState).

Ideas?

0


source to share


2 answers


CreateChildControls is mainly for controls that have child controls. RequiredFieldValidator is like the sibling in TextBox.

Here is the code that works for me:

public class RequiredTextBox : TextBox
    {
        private RequiredFieldValidator _req;
        private string _errorMessage;

        public string ErrorMessage
        {
            get { return _errorMessage; }
            set { _errorMessage = value; } 
        }

        protected override void OnInit(EventArgs e)
        {
            _req = new RequiredFieldValidator();
            _req.ControlToValidate = this.ID;
            _req.ErrorMessage = _errorMessage;
            Controls.Add(_req);
            base.OnInit(e); 
        }       

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            base.Render(writer);
            _req.RenderControl(writer); 
        }
    }

      

And here is this ASP.NET page behind:



 protected void SubmitClick(object sender, EventArgs e)
        {
            if(Page.IsValid)
            {
                // do something
            }
        }

      

And here is the ASPX code:

 <MyControl:RequiredTextBox runat="server" ErrorMessage="Name is required!" ID="txtName"></MyControl:RequiredTextBox>

    <asp:Button ID="Btn_Submit" runat="server" Text="Submit" OnClick="SubmitClick" /> 

      

+1


source


Validators must inherit from BaseValidator.



0


source







All Articles