Click here to Skip to main content
6,595,854 members and growing! (17,414 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » General     Beginner License: The Code Project Open License (CPOL)

Creating a Read Only DropDownList

By DSpazman

This explains how to extend a DropDownList to give it a ReadOnly attribute.
C# 1.0, C# 2.0, C# 3.0, .NET, ASP.NET, Dev
Version:2 (See All)
Posted:4 Jun 2008
Views:13,474
Bookmarked:11 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
4 votes for this article.
Popularity: 2.05 Rating: 3.40 out of 5

1
1 vote, 25.0%
2

3
2 votes, 50.0%
4
1 vote, 25.0%
5

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


Member

Occupation: Web Developer
Location: United States United States

Other popular ASP.NET Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralPostback problem Pinmembersykiemikey6:31 4 May '09  
GeneralAlternate option PinmemberRenish@IT0:02 27 Feb '09  
GeneralGood idea Pinmemberdave.dolan6:42 17 Dec '08  
GeneralInteresting approach. PinmemberRajib Ahmed18:01 5 Jun '08  
QuestionHuh? PinmemberPIEBALDconsult9:10 4 Jun '08  
AnswerRe: Huh? PinmemberDSpazman9:44 4 Jun '08  
GeneralRe: Huh? Pinmember leppie 10:22 4 Jun '08  
GeneralRe: Huh? PinmemberSteven Berkovitz11:59 4 Jun '08  
GeneralRe: Huh? PinmemberPIEBALDconsult13:07 4 Jun '08  
GeneralRe: Huh? PinmemberDSpazman13:46 4 Jun '08  
GeneralRe: Huh? PinmemberPIEBALDconsult14:41 4 Jun '08  
GeneralRe: Huh? PinmemberMohammed Gouda2:47 17 Jun '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 4 Jun 2008
Editor: Smitha Vijayan
Copyright 2008 by DSpazman
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project