具有内置Validator的自定义文本框:服务器端验证未触发 [英] Custom TextBox with built-in Validator: server side validation not firing

查看:82
本文介绍了具有内置Validator的自定义文本框:服务器端验证未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的课:

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);
   }
}

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

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?

所以Page.IsValid属性始终返回true(我什至尝试过事先显式调用Page.Validate())。

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

经过一番挖掘,我发现验证器的init方法应该将验证器添加到页面中,但是由于我的构建方式,我没有认为这曾经发生过。因此,客户端验证有效,而服务器端验证无效。

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.

我已经尝试过:

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

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

但是验证器当然是这里为null(有时不是必需的,因此不应该添加它)...但是OnInit()对于我做出这些决定确实为时过早(例如,不会从ViewState加载Required属性)。

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

想法?

推荐答案

CreateChildControls基本上用于具有子项的控件。 RequiredFieldValidator就像TextBox的同级对象。

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); 
        }
    }

下面是ASP.NET页: / p>

And here it the ASP.NET page behind:

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

这是ASPX代码:

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

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

这篇关于具有内置Validator的自定义文本框:服务器端验证未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆