具有内置Validator的自定义TextBox:服务器端验证未触发

时间:2021-11-18 16:05:05

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?

这已经在内部应用程序中工作了一段时间,其中始终启用了javascript。我最近注意到上游的javascript错误可能会阻止验证程序触发,因此服务器端验证应该启动...对吗?对?

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

所以Page.IsValid属性总是返回true(我甚至尝试在事先显式调用Page.Validate())。

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).

但是当然验证器在这里是空的(有时它不是必需的,因此不应该添加)...但OnInit()对我来说真的很早就做出了这些决定(必须从ViewState加载必需的属性)例如)。

Ideas?

2 个解决方案

#1


1  

The CreateChildControls is basically for the controls that have childs. RequiredFieldValidator is like a sibling to TextBox.

CreateChildControls基本上用于具有子项的控件。 RequiredFieldValidator就像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:

这里是后面的ASP.NET页面:

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

And here is the ASPX code:

这是ASPX代码:

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

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

#2


0  

Validators have to inherit from BaseValidator.

验证器必须从BaseValidator继承。

#1


1  

The CreateChildControls is basically for the controls that have childs. RequiredFieldValidator is like a sibling to TextBox.

CreateChildControls基本上用于具有子项的控件。 RequiredFieldValidator就像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:

这里是后面的ASP.NET页面:

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

And here is the ASPX code:

这是ASPX代码:

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

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

#2


0  

Validators have to inherit from BaseValidator.

验证器必须从BaseValidator继承。