5,276,406 members and growing! (18,242 online)
Email Password   helpLost your password?
Web Development » Custom Controls » General     Advanced License: The Code Project Open License (CPOL)

DropDownList with support of optgroup element

By Petr Koutny

Asp.net lacks for support of option groups since its first release. My goal was to fill out this hole in .net framework.
C# (C# 2.0, C#), CSS, HTML, Windows (Windows, WinXP, Win2003, Vista), ASP.NET

Posted: 24 Mar 2008
Updated: 24 Mar 2008
Views: 2,718
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 1.34 Rating: 1.92 out of 5
3 votes, 60.0%
1
0 votes, 0.0%
2
1 vote, 20.0%
3
0 votes, 0.0%
4
1 vote, 20.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

I've recently noticed that many programmers miss functionality of option groups in .net framework select controls. After a while I decided to write my own control, that works similar to System.Web.UI.DropDownList control built in .net framework. My control is available for .net framework 2.0 or later.

Background, W3 recommendation

When working with DropDownGroupableList control, please keep in mind restrictions related to option groups and option items defined by w3 consorcium. See http://www.w3.org/TR/html4/interact/forms.html#h-17.6. Good website programmer is not defined only through effectivity of his program code :-)

Data source types

Data can be delivered in four different ways:

  • Inline items definition
    Note: this way enables you to specify option groups as well as option items
  • Manual filling in Items collection in your code
    Note: notice AddItem and AddGroup methods exposed by Items collection
  • DataSource property containing valid IEnumerable data
  • Declarative data source provided by control referenced by DataSourceID property

Provided data, declarative or manual through DataSource property, may be handled in normal way or hierarchically - read following paragraph.

Hierarchical data source support

If provided data source implements IHierarchicalEnumerable then DropDownGroupableList works little differently according to data processing. It is based on theory that option groups together with items represent some kind of two-level hierarchy.

You can control hierarchical data processing by these settings:

  • IgnoreFirstBoundParent - this instructs list control that it should ignore the first bound parent and binds its children instead. This means that first parent is root of data that are really bound. This can helpful when binding Site map that having one root node that plays no role inside site navigation. Default value is false.
  • BindEmptyParentAsListItem - this setting controls how are processed empty parents. There are generally two ways you can handle empty parent: create option group with no child options (then it cannot be selected) or create option item you can select. Default value is true.
  • DataGroupTextField, DataGroupTextFormatString and DataGroupIDField - similar to well-known properties from DropDownList but refers to option groups attributes.

Common properties

If you are experienced with System.Web.UI.WebControls.DropDownList control (and you should be when reading this article), you probably know its functionality and behavior. Here are listed some important and commonly used capabilities that my control has in common with standard DropDownList:

  • AppendDataBoundItems - value that specifies if Items collection is cleared before binding data.
  • AutoPostBack - sends form back to server after selected item is changed.
  • SelectedItemChanged event - occurs when selected item changes, note this event is not related to option groups since they cannot be selected
  • CausesValidation, ValidationGroup - properties that work together to ensure controls validity
  • DataTextField, DataValueField - names of bound object properties that contains Text property value or Value property value, respectively.

Differences against standard DropDownList

SelectedValue

When you set the value of property SelectedValue to value that corresponds to more than one item (e.g. you inserted more items with same value), then exception is thrown.

ListItem.Enabled

Enabled property of System.Web.UI.WebControls.ListItem class influences its visibility on page. I decided to change this behavior so setting this value to false makes it disabled via disabled html attribute but its still being rendered.

Items collection

This collection definition works with abstract OptionItemBase class that is implemented by OptionListItem and OptionGroupItem class. This enables my list control to contain option items as well as option groups at top level. OptionGroupItem class then may contain only OptionListItem class instances, no inner groups are allowed since it is not part of w3 recommendation.

Samples

Attached zip file contains following sample pages:

  • DynamicDataSource.aspx - demonstrates dynamic filling in from code-beside
  • InlineDataDefinition.aspx - demonstrates inline Items definition
  • HierarchicalDataSource.aspx - shows capabilities of hierarchical data source functionality

Conclusion

It took me some noticeable time to create and debug this component so I hope its gonna be helpful for all asp.net programmers. Feel free to use it :-)

License

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

About the Author

Petr Koutny


ASP.NET programmer in W.D.A. Czech, s.r.o. company.
Occupation: Web Developer
Location: Czech Republic Czech Republic

Other popular Custom Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralWhy you didn't attach the source code?memberbonifacy22:33 7 May '08  
GeneralRe: Why you didn't attach the source code?memberisunshine6:12 14 May '08  
GeneralRe: Why you didn't attach the source code?memberPetr Koutny13:51 14 May '08  

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

PermaLink | Privacy | Terms of Use
Last Updated: 24 Mar 2008
Editor:
Copyright 2008 by Petr Koutny
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project