The Peak is in sight

Recently I had a problem with a required field validator and a DropDownList in a composite server control I had created. I'm trying to implement a Self-Validating input control that changes its implementation based on the datatype specified by the developer.  Numbers, strings, and datetimes are rendered as a textbox field for user entry.  Booleans are optionally rendered as a radiobuttonlist or CheckBox.  Lists are rendered in a DropDownList.  In my parlance, these are referred to as the Value controls.  In addition, the ValidatedInputControl renders a label and uses RequiredField and CompareValidators to check the output.  I had to develop a custom RegEx to validate datetimes since the standard CompareValidator can only validate dates. Finally, if the developer specifies that null values are allowed, a checkbox will be rendered beside the Value control.  Checking the box disables the Value Control, making its value null. Again, the rendering is determined dynamically by the datatype of the control.

Since the RequiredFieldValidator is returning Not Valid to the Page during page validation if I'm rendering a dropdownlist, I had a serious problem. I thought of not including the RequiredFieldValidtor if I'm using a dropdownlist, but that would actually create a lot of special code in the case where displaying a list. I've decided to abstract the ValidatedInputControl into component pieces.  I'll create a ValidatedTextBox control that accepts Numeric, String, and datetime values, a ValidatedBoolean Control that optionally renders a radiobuttonlist or checkbox, and a ValidatedListControl which will basically be a dropdownlist, a label, and an isnull checkbox if necessary (the checkbox isn't necessary if the dropdownlist contains a value that is already null.

Finally, I'll modify the ValidatedInputControl so that it renders one of the above child controls based on the datattype.

Side Note:

When developing a composite control that changes the type of child control rendered, I've found it necessary to set Me.ChildControlsCreated = False anytime a property that affects the type of Value control I render is set.  For example, changing the datatype from Integer to Boolean requires removing the textbox from the controls collection, and replacing it with a RadioButtonList.  Setting Me.ChildControlsCreated = False ensures that CreateChildControls is called.  This also means that I can't use the child control's viewstate to store properties relevant to it, such as style or whatnot. I have to store all properties related to the childcontrols in the local viewstate, and apply them during RenderContents. I haven't seen any examples of this type of control yet, so if there's demand for it, I may write an article demonstrating how to create one.

Of course, if someone knows a better approach to the problem of a dynamically rendered composite control, I'll be glad to hear it!

Published Monday, March 15, 2004 9:58 AM by taganov
Filed under:

Comments

# re: The Peak is in sight

Friday, May 07, 2004 5:21 PM by Tommy

Chris, this post came up in a Google search for "viewstate dropdownlist composite controls". I am implementing a Reporting Services web-based UI as well, and have been working on a custom composite control like you describe. My sticking point is that I do not see why the dynamic control cannot save its viewstate; when it comes back on a post, it's still the same type (since this can be explicitly stored in ViewState), and so should be able to restore itself. This in particular with values for a list. The dynamically-added ASP server controls do not even show ViewState usage in a page trace, but for dates I use third-party eWorldui.net's date picker, which is using viewstate in the page. Anyway. Thanks for the heads up on what you've seen.

-Tommy McLeod

Leave a Comment

(required) 
(required) 
(optional)
(required)