Custom TextBox with built-in Validator: server side validation not firing

六月ゝ 毕业季﹏ 提交于 2019-12-06 16:16:56

问题


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 has been working for a while in a internal application where javascript is always enabled. I recently noticed that an upstream javascript error can prevent the validators from firing, so the server side validation should kick in... right? right?

So the Page.IsValid property always returns true (I even tried explicitly calling Page.Validate() before-hand).

After some digging, I found that the validator init method should add the validator to the page, but due to the way I'm building it up, I don't think this ever happens. Thus, client side validation works, but server side validation does not.

I've tried this:

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

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

But of course the validator is null here (and sometimes it's not required so it shouldn't be added)... but OnInit() is really early for me to make those decisions (the Required property won't have been loaded from ViewState for example).

Ideas?


回答1:


The CreateChildControls is basically for the controls that have childs. RequiredFieldValidator is like a sibling to 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 it the 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" /> 



回答2:


Validators have to inherit from BaseValidator.



来源:https://stackoverflow.com/questions/302252/custom-textbox-with-built-in-validator-server-side-validation-not-firing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!