Click here to Skip to main content
15,867,568 members
Articles / Web Development / ASP.NET

Creating a Read Only DropDownList

Rate me:
Please Sign up or sign in to vote.
3.45/5 (5 votes)
4 Jun 2008CPOL1 min read 85.8K   943   15   12
This explains how to extend a DropDownList to give it a ReadOnly attribute.

Introduction

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.

C#
using System.Web.UI.WebControls

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

    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
        if (_readOnly)
        {
            tb.Text = this.Text;
            tb.ReadOnly = true;
            tb.RenderControl(writer);
        }
        else
            base.Render(writer);
    }
}

Setup

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).

History

To-Do: Might need some error checking in the Render method.

License

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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralPostback problem Pin
sykiemikey4-May-09 5:31
sykiemikey4-May-09 5:31 
GeneralAlternate option Pin
Renish@IT26-Feb-09 23:02
Renish@IT26-Feb-09 23:02 
If you do not want to search and replace every DropDownList.Enable with ReadOnly. Then you can use this, just replace all 'DropDownList' with 'CustomeDropDownList' in HTML and every thing else remains same.

public class CustomDropDownList : DropDownList
{
	protected override void Render(System.Web.UI.HtmlTextWriter writer)
	{
		if (!this.Enabled)
		{
			TextBox oTextBox = new TextBox();
			oTextBox.Text = this.SelectedItem.Text;
			oTextBox.Width = new Unit(this.Width.Value - 6, UnitType.Pixel);
			oTextBox.ReadOnly = true;
			oTextBox.RenderControl(writer);
		}
		else
		{
			base.Render(writer);
		}
	}
}

GeneralGood idea Pin
dave.dolan17-Dec-08 5:42
dave.dolan17-Dec-08 5:42 
GeneralInteresting approach. Pin
Rajib Ahmed5-Jun-08 17:01
Rajib Ahmed5-Jun-08 17:01 
QuestionHuh? Pin
PIEBALDconsult4-Jun-08 8:10
mvePIEBALDconsult4-Jun-08 8:10 
AnswerRe: Huh? Pin
DSpazman4-Jun-08 8:44
DSpazman4-Jun-08 8:44 
GeneralRe: Huh? Pin
leppie4-Jun-08 9:22
leppie4-Jun-08 9:22 
GeneralRe: Huh? Pin
Steven Berkovitz4-Jun-08 10:59
Steven Berkovitz4-Jun-08 10:59 
GeneralRe: Huh? Pin
PIEBALDconsult4-Jun-08 12:07
mvePIEBALDconsult4-Jun-08 12:07 
GeneralRe: Huh? Pin
DSpazman4-Jun-08 12:46
DSpazman4-Jun-08 12:46 
GeneralRe: Huh? Pin
PIEBALDconsult4-Jun-08 13:41
mvePIEBALDconsult4-Jun-08 13:41 
GeneralRe: Huh? Pin
Muhammad Gouda17-Jun-08 1:47
Muhammad 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.