Click here to Skip to main content
Licence CPOL
First Posted 4 Jun 2008
Views 32,907
Downloads 399
Bookmarked 14 times

Creating a Read Only DropDownList

By | 4 Jun 2008 | Article
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.

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)

About the Author

DSpazman

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralPostback problem Pinmembersykiemikey5:31 4 May '09  
GeneralAlternate option PinmemberRenish@IT23:02 26 Feb '09  
GeneralGood idea Pinmemberdave.dolan5:42 17 Dec '08  
GeneralInteresting approach. PinmemberRajib Ahmed17:01 5 Jun '08  
QuestionHuh? PinmemberPIEBALDconsult8:10 4 Jun '08  
AnswerRe: Huh? PinmemberDSpazman8:44 4 Jun '08  
GeneralRe: Huh? Pinmember leppie 9:22 4 Jun '08  
GeneralRe: Huh? PinmemberSteven Berkovitz10:59 4 Jun '08  
GeneralRe: Huh? PinmemberPIEBALDconsult12:07 4 Jun '08  
GeneralRe: Huh? PinmemberDSpazman12:46 4 Jun '08  
GeneralRe: Huh? PinmemberPIEBALDconsult13:41 4 Jun '08  
GeneralRe: Huh? PinmemberMohammed Gouda1:47 17 Jun '08  
PIEBALDconsult wrote:
but what should a readonly DropDownList do

 
By luck, I have a scenario where a readonly DropDownListBox is usefull:
 
I have a form with a DataGrid of 3 fields messengerId (hidden), messengerName,and messengerLocation
When the user selects a Messenger from the DataGrid, the messengerName shall appear on the form, and messengerId shall be stored in the database
So,
DropDownListBox will be a good choice that will read the ID in the DataValueField and display the Name in the DataTextField
But, I do not need the user to change the meesenger directly from the ListBox, cause he must give attention to the field messengerLocation before assigning him a task.
Otherwise, I would need 2 textboxes, one for ID and the other for Name, which will require more code for handling
 
Mohammed Gouda
foreach(Minute m in MyLife)
myExperience++;

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.

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