Click here to Skip to main content
13,199,852 members (72,791 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 4 Jun 2008

Creating a Read Only DropDownList

, 4 Jun 2008
Rate this:
Please Sign up or sign in to vote.
This explains how to extend a DropDownList to give it a ReadOnly attribute.


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.

A TextBox set as Enabled, Disabled, or 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.

using System.Web.UI.WebControls

public class DropDownListReadOnly : DropDownList
    private bool _readOnly;
    private TextBox tb = new TextBox();
    public bool ReadOnly
            return _readOnly;
            _readOnly = value;

    protected override void Render(System.Web.UI.HtmlTextWriter writer)
        if (_readOnly)
            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 Render method.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralPostback problem Pin
sykiemikey4-May-09 5:31
membersykiemikey4-May-09 5:31 
GeneralAlternate option Pin
Renish@IT26-Feb-09 23:02
memberRenish@IT26-Feb-09 23:02 
GeneralGood idea Pin
dave.dolan17-Dec-08 5:42
memberdave.dolan17-Dec-08 5:42 
GeneralInteresting approach. Pin
Rajib Ahmed5-Jun-08 17:01
memberRajib Ahmed5-Jun-08 17:01 
QuestionHuh? Pin
PIEBALDconsult4-Jun-08 8:10
memberPIEBALDconsult4-Jun-08 8:10 
AnswerRe: Huh? Pin
DSpazman4-Jun-08 8:44
memberDSpazman4-Jun-08 8:44 
PIEBALDconsult wrote:
private TextBox tb = new TextBox();

Why not a string?

private string tb = "";

1) A TextBox looks more like a Dropdownlist.
2) Assuming your website has CSS styles for TextBoxes already, it'll pick up those styles and not need to be personally styled by you.

If you wished, you could change it to a string, create whatever formatting you wanted for it, if you want to box it in, display a picture, whatever. I was just doing this for an example.

PIEBALDconsult wrote:
And at any rate, the intent of your article is unclear.
What are you trying to accomplish?

3) The intent is to create a DropDownList with a ReadOnly attribute. If you look at a textbox, and make one normal, one ReadOnly, and one Disabled, you have 3 different looks. The ReadOnly has a background color, and can not be changed, but you can select the text inside. the Disabled has an aweful font and background color that makes it very hard to read, and you cannot select the Text inside it.

DropDownLists only have an Enabled and Disabled mode, not a ReadOnly. This control extends the DropDownList to include a ReadOnly Version.
GeneralRe: Huh? Pin
leppie4-Jun-08 9:22
member leppie 4-Jun-08 9:22 
GeneralRe: Huh? Pin
Steven Berkovitz4-Jun-08 10:59
memberSteven Berkovitz4-Jun-08 10:59 
GeneralRe: Huh? Pin
PIEBALDconsult4-Jun-08 12:07
memberPIEBALDconsult4-Jun-08 12:07 
GeneralRe: Huh? Pin
DSpazman4-Jun-08 12:46
memberDSpazman4-Jun-08 12:46 
GeneralRe: Huh? Pin
PIEBALDconsult4-Jun-08 13:41
memberPIEBALDconsult4-Jun-08 13:41 
GeneralRe: Huh? Pin
Mohammed Gouda17-Jun-08 1:47
memberMohammed Gouda17-Jun-08 1:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171020.1 | Last Updated 4 Jun 2008
Article Copyright 2008 by DSpazman
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid