When a control is disabled, it can not be styled in IE, and the default style sucks. So, I searched for a long time trying to figure out the best way of creating a dropdownlist control that has a ReadOnly property, which would be much more readable to a user, while still maintaining all the benefits of a
DropDownList control. After much searching, I decided to try the easy road, and came up with this control.
TextBox set as
ReadOnly will look three different ways. This allows a
DropDownList to have that same behavior.
Using the code
Basically, I decided to create my own control derived from
DropDownList. I had it internally contain a
TextBox, and when it went to be rendered, it just rendered the selected item, or the dropdown list, depending on if the
ReadOnly property was set.
public class DropDownListReadOnly : DropDownList
private bool _readOnly;
private TextBox tb = new TextBox();
public bool ReadOnly
_readOnly = value;
protected override void Render(System.Web.UI.HtmlTextWriter writer)
tb.Text = this.Text;
tb.ReadOnly = true;
The easiest way of using this project is to create your own Web Control Library, and then add this class to your library. You can then insert the control into the VS2005 Designer by the normal method (right click on the Toolbox, pick the Add tab; then, right click the tab and pick Choose Items, and browse to the compiled DLL).
To-Do: Might need some error checking in the