![]() |
Web Development »
Custom Controls »
General
Advanced
License: The Code Project Open License (CPOL)
DropDownList with support for the optgroup elementBy Petr KoutnyASP.NET lacks for support of option groups since its first release. My goal is to fill out this hole in the .NET framework. |
C# (C# 2.0), CSS, HTML, Windows (WinXP, Win2003, Vista), ASP.NET, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
I've recently noticed that many programmers miss the functionality of option groups in the .NET Framework selection controls. After a while, I decided to write my own control, that works similar to the System.Web.UI.DropDownList control available in the.NET framework. My control is available for .NET framework 2.0 or later.
When working with the DropDownGroupableList control, please keep in mind the restrictions related to option groups and option items defined by the W3 Consortium. See this article. A good website programmer is not defined only through the effectiveness of his program code :-)
Data can be delivered in four different ways:
Note: this enables you to specify option groups as well as option items.
Items collection in your code.Note: Notice the AddItem and AddGroup methods exposed by the Items collection.
DataSource property containing valid IEnumerable data.DataSourceID property.Provided data, declarative or manual through the DataSource property, may be handled the normal way, or hierarchically - read the following paragraph.
If the provided data source implements IHierarchicalEnumerable, then DropDownGroupableList works a little differently according to data processing. It is based on the 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 a list control that it should ignore the first bound parent and bind its children instead. This means that the first parent is the root of the data that is really bound. This can be helpful when binding site maps that have one root node that plays no role inside site navigation. The default value is false.BindEmptyParentAsListItem - this setting controls how to process empty parents. There are generally two ways you can handle empty parents: create an option group with no child options (then it cannot be selected), or create an option item you can select. The default value is true.DataGroupTextField, DataGroupTextFormatString, and DataGroupIDField - similar to the well-known properties from DropDownList, but refers to option group attributes.If you are familiar with the 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 the standard DropDownList:
AppendDataBoundItems - value that specifies if the Items collection is cleared before binding data.AutoPostBack - sends form back to the server after the selected item is changed.CausesValidation, ValidationGroup - properties that work together to ensure the control's validityDataTextField, DataValueField - names of bound object properties that contain the Text property value or the Value property value, respectively.SelectedItemChanged - occurs when the selected item changes; note this event is not related to option groups since they cannot be selected.When you set the value of the property SelectedValue to a value that corresponds to more than one item (e.g., you inserted more items with the same value), then an exception is thrown.
The Enabled property of the System.Web.UI.WebControls.ListItem class influences its visibility on the page. I decided to change this behavior, so setting this value to false makes it disabled via the disabled HTML attribute, but it is still being rendered.
This collection definition works with the abstract OptionItemBase class that is implemented by the OptionListItem and OptionGroupItem classes. This enables my list control to contain option items as well as option groups at the top level. The OptionGroupItem class then may contain only OptionListItem class instances, no inner groups are allowed since it is not part of the W3 recommendation.
The attached zip file contains the following sample pages:
Items definition.It took me some noticeable time to create and debug this component, so I hope it will be helpful for all ASP.NET programmers. Feel free to use it :-)
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 24 Mar 2008 Editor: Smitha Vijayan |
Copyright 2008 by Petr Koutny Everything else Copyright © CodeProject, 1999-2009 Web18 | Advertise on the Code Project |