Introduction
The ASP.NET ComboBox idea is simple; it is a custom composite control that contains two controls (TextBox, Image). The ComboBox Control inherits directly from System.Web.UI.WebControls.ListControl to inherit all design time capabilities; after all it is a ListControl like all other List Controls such as DropDownList, ListBox, RadioButtonList etc.
ASP.NET ComboBox Control Layout
I've chosen a div to align the ASP.NET ComboBox child controls. The control is rendered as a container div and child controls placed within that container. ASP.NET ComboBox is using the base content rendered by its base class ListControl.
Figure 1. ASP.NET ComboBox Layout

Why did I choose ListControl?
The ListControl by default will render <select> HTML tag element. ASP.NET ComboBox will render its selection options as ListBox control which also inherits from the ListControl, but will allow only one option to be selected. As we are going to simulate a ListBox rendering, a property named Rows is added to ASP.NET ComboBox to limit the number of rows inside the list and it will enable scrolling if the number exceeds the Rows. The ListControl has 90% of my needed functionality implemented and comes with a lightweight viewstate. As for styling, I just need the fore color, background color, Font styles and the Css Class. The ListBox doesn't allow border styles, so even if you try to apply them it will not work.
ASP.NET ComboBox Control JavaScript
Initially the control visibility style is hidden. The visibility is toggled to visible when the TextBox or Image is clicked. In other words, when the first row is clicked. When the ListBox itself is clicked while visible of course the selected item text is displayed on the TextBox and the ListBox is hidden again. The same interaction is done when the Enter or Esc keyboard buttons are pressed. Below are the JavaScript functions used to hide and show ListBox:
Listing 1. JavaScript functions to hide and show list of options
Note: All JavaScript functions are available on ScsComboBoxUtil.js file on the source code attached to the article.
ASP.NET ComboBox Support for AutoPostBack
I didn't try to implement handling for PostBack because it is already implemented in the ListBox or TextBox child controls. So I decided to delegate this to the TextBox control. If you enabled the AutoPostBack Property, and change the selection on the ListBox, the TextBox Text property will set programmatically through the JavaScript. However this will not invoke the onchange client event of the TextBox, so I enforced the onchange client event to fire if the Text is changed on the TextBox.
Listing 2. JavaScript to display selected item text on the TextBox
Listing 3. C# code for AutoPostBack overridden property, delegate AutoPostBack to the TextBox Control
Implementing IPostBackDataHandler
It is important to handle the postback data, since this could not be delegated to any child controls; we need to post the selected value as well as the text. Below is the implementation of the IPostBackDataHandler interface.
Listing 4. IPostBackDataHandler implementation
Delegate Properties to child controls
As the ListBox and TextBox have most of the properties needed already implemented, I delegated my needed properties to these controls properties. Some properties are handled by the control itself and not delegated, such as the Rows property.
Listing 5. Property delegation
Implementing PerformDataBinding method
I've added an additional feature to the ASP.NET ComboBox control, this feature enables you to insert a default option at the top of the ListItems. This is simple in case of a declarative list of options, but will require additional handling incase of data binding. Simply before binding data from the data source to the control, I add the default item if enabled to the top and all binded options will be added afterwards.
Listing 6. PerformDataBinding Implementation
Note: This implmenetation is almost similar to the implementation written in the base PerformDataBinding method in ListControl.
Control Style Support
I'm not going through this point right now, but I'll provide another article that describes how to support special styles for your controls. Basically we need to support the style for TextBox, Image and ListBox child controls. So the control will contain three properties, TextBoxStyle, ListBoxStyle and ImageStyle. In the next article I'll talk about these three properties.
Web Resources
The ASP.NET ComboBox control contains some JavaScript code, this code is wrapped in a ".js" file. The file is added as an embedded resource in the control assembly and the assembly Meta data, this file is marked as Web Resource file.
It worth mentioning also that the Image used in the control is stored as Web Resource too. If you do not provide the ImageUrl property, the control will render its default embedded image. This is commonly used in all third party controls to provide consistent look and feel for their controls.
To read a tip about how to register a file as web resource and render it with your control, please read my blog entry: System.Web.UI.WebResourceAttribute Issue
Control Rendering
I have applied control rendering by overriding the AddAttributesToRender and Render methods. I've also made a helper method to prepare the child controls for rendering. You may refer to the attached download file to see how these methods are implemented.
Listing 7. Render Methods
Composite Control Tips
Be sure to create you child control in the CreateChildControls method, and to call EnsureChildControls method before accessing your child controls in any property as shown in the above code, except in the Render method, as in that time all child controls would be created and ready.
Listing 8. Common Implementation for CreateChildControls
Implement the INamingContainer interface
Any control that implements this interface creates a new namespace in which all child control ID attributes are guaranteed to be unique within the entire page. It is interesting to know that this is a marker interface that has no methods or properties to implement.
Design Time and Code Editor support Tips
You'll find that some of the inherited properties are not used, or could be replaced with others. It is better to hide such properties from design time and from code editor. To do this, mark these properties with the following attributes.
Listing 9. Design Time and Code Editor Support
ASP.NET ComboBox What might not be good
The generated HTML of these controls is not 100% supported with Opera or other browsers, I've tested it with Opera, it is working but sometimes the behavior is not as expected. Also the positioned list box under the text box, sometimes is not 100% accurate. You may find some other issues as it has not tested thorouhly as it should be. I've added it to a production project and it didn't so far present in issues.
Conclusion:
The ASP.NET ComboBox is a simple control that is built to satisfy specific needs. It doesn't include many of the advanced features available in third party controls. It is based on the simple idea to hide the ListBox under the TextBox, and to show it only on demand. It can be enhanced to support auto suggest e.g. you type the first characters of the word and the ListBox automatically selects the first match. It also doesn't support multi column drop downs, which could be another enhancement. For bugs or enhancement, kindly contact me at mohammed_moses@yahoo.co.uk
Downloads
ASP.NET ComboBox Version 1.51
ASP.NET ComboBox Version 1.5
ASP.NET ComboBox Version 1.0
History
- Version 1.5:
- Drop down list is only displayed when the drop down arrow is clicked
- Text box is enabled to enter free text by setting ScsComboBox.EnableFreeText Property
- Support for key board up and down
- Support for auto complete by searching the drop down list for match (type text and press tab key)
Bugs Fixed:
- Javascript error when placed inside GridView
- Drop down Image not displayed when placed inside GridView
- version 1.0:
About Muhammad Mosa
 |
Sorry, no bio is available
This author has published 8 articles on DotNetSlackers. View other articles or the complete profile here.
|
You might also be interested in the following related blog posts
WPF DataGrid as ComboBox Dropdown
read more
Multiple-Selection ComboBox for Silverlight
read more
WPF Release History : Q2 2009 (version 2009.2.701)
read more
ASP.NET combobox dropdownlist with images
read more
How to Customize the New AjaxControlToolkit Tools
read more
Silverlight Release History : Q2 2009 (version 2009.2.701)
read more
Reporting Release History : Q2 2009 SP1 (version 3.1.9.807)
read more
ORM Release History : Q2 2009 (2009.2.701.5)
read more
Menu Merge (RadControls for WinForms)
read more
A Default Command for Silverlight
read more
|
|
Please login to rate or to leave a comment.