Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Right Way to do User Controls

Hi there,

I have decided to hack apart the mega-forms in my application into subsections which will be UserControls.

Each subsection is used sometimes for input (with DropDownLists, TextBoxes, etc.) and sometimes for display with Labels.  Other than that the Controls are identical.

My question is do I need to do ControlAInput, ControlBInput and ControlADisplay, ControlBDisplay for the input/display versions or is there some clever way to use the bits that are the same and dynamically decide the bits that are not the same at runtime in the Control?

Or am I going about this whole thing the wrong way?



Thomas David Baker
Tuesday, December 9, 2003

Subclass all the contituent controls you use (e.g. TextBox, ListBox etc) and give them something like a "DisplayOnly" property. Override Render(), when DisplayOnly is set to true render just a label or something, otherwise delegate to the base class's Render() method...

Duncan Smart
Tuesday, December 9, 2003

That's quite an effective way of getting what I want but it does seem like a big solution for a little problem. It's the same as my recurring problem of there not being a common interface/ancestor for the .Text property of labels and TextBoxes making it impossible to write methods that will deal with either.  To solve this too I am going to implement your idea but it just seems like I am doing what the library writers should have done. 

Any idea why they didn't make more interfaces/common ancestors in WebControls?

Thomas David Baker
Saturday, December 13, 2003

*  Recent Topics

*  Fog Creek Home