Click here to Skip to main content
Click here to Skip to main content

Creating a Read Only DropDownList

, 4 Jun 2008 CPOL
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

Comments and Discussions

GeneralPostback problem Pinmembersykiemikey4-May-09 6:31 
GeneralAlternate option PinmemberRenish@IT27-Feb-09 0:02 
GeneralGood idea Pinmemberdave.dolan17-Dec-08 6:42 
GeneralInteresting approach. PinmemberRajib Ahmed5-Jun-08 18:01 
QuestionHuh? PinmemberPIEBALDconsult4-Jun-08 9:10 
AnswerRe: Huh? PinmemberDSpazman4-Jun-08 9:44 
GeneralRe: Huh? Pinmember leppie 4-Jun-08 10:22 
GeneralRe: Huh? PinmemberSteven Berkovitz4-Jun-08 11:59 
GeneralRe: Huh? PinmemberPIEBALDconsult4-Jun-08 13:07 
GeneralRe: Huh? PinmemberDSpazman4-Jun-08 13:46 
Yes, it is using the TextBox, to render. If you tried out the control, you'd see it renders as either a dropdownlist or a textbox, depending if ReadOnly is enabled or not. If ReadOnly is selected, it renders the TextBox.
GeneralRe: Huh? PinmemberPIEBALDconsult4-Jun-08 14:41 
GeneralRe: Huh? PinmemberMohammed Gouda17-Jun-08 2:47 

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

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

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