Creating a custom textbox control without using a composite? RRS feed

  • Question

  • User-1538193166 posted

    I originally wrote a RequiredTextbox control which has built-in validation as a custom control subclassed from a TextBox. The problem is, from everything I've read, having a validator being rendered in such a control is apparently a pain...something about the naming container not working correctly. I've not been able to find specific information on what the problem is, though. Can someone walk me through what exactly is the technical problem with having a textbox subclass that renders its own validator?


    Thursday, October 11, 2007 12:50 PM


  • User847130323 posted

    There are two problems.

    1.Textbox inherits from WebControl.   Webcontrol does not implement INamingContainer.  Therefore, if you were to place multiple RequiredTextBoxes in the same page, its possible that the RequiredFieldValidators would have ID conflicts.  However there is nothing stoping you from Implementing INamingContainer yourself, so that really isn't a problem.

    2.   In the WebControl base class, the render method Calls , RenderContents(), Which calls the Control Base Class Render(), which in turn Calls RenderChildren() Which in turn will render all of the child controls.  However The TextBox overrites Render() of WebControl.  In TextBox version of Render, all it does is render the textbox.  It bypasses all of the functionality that would allows the child controls to be rendered.  So you have to manually call this functionality.  Not exactly a good design pattern.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 12, 2007 6:24 PM