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

I have a class that looks like this:

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

   protected override void CreateChildControls()

      _validator = new RequiredFieldValidator();
      _validator.ControlToValidate = this.ID;

   public override void Render(HtmlTextWriter 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()

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



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;

        protected override void Render(System.Web.UI.HtmlTextWriter writer)

And here it the ASP.NET page behind:

 protected void SubmitClick(object sender, EventArgs e)
                // 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" />

Need Your Help

Animating a simple 2D line in vanilla JS


I've been reading through guides on the internet, but I haven't been able to find a way to animate a line from one position to a new position.

How to iterate through file line by line?

python-2.7 iterator iteration bioinformatics

I have files with lines of input which I compare to eachother. I have simplified my code alot and obviously it isnt in working python but the main bits of important are the first line for iteration...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.