Skip to main content
Email Password   helpLost your password?

Introduction

Using server controls in ASP.NET is pretty slick. Having a robust programming model to interact with your HTML elements surely makes life nice. While ASP.NET ships with all of the most common server controls that you might need, it is always fun to add to your toolbox. This ComboBox control mimics the functionality of ComboBoxes from the WinForms world - letting the user select from a pre-defined list of choices or type in a new value.

Alternatives

This is not the first or only implementation of ComboBox functionality on the internet, or even on Code Project. This article provided one solution, but it was based on Microsoft behaviors, making it IE-specific, and it used custom images to simulate the dropdown arrow.

This article provided a solution that visually looked like a real <SELECT> box, but it relied on behaviors as well - but I wanted a complete cross-browser support and didn't want to deal with the extra .htc file.

I also stumbled across this implementation from MetaBuilders. It was cross-browser compatible and degraded gracefully, but it still didn't look like a normal HTML element and it had an annoying 'flash' as it re-drew in the browser when it was loaded.

Finally, I found a very slick implementation from Opinionated Geek that seemed to be what I was looking for. It looked just like a real <SELECT> box and seemed to degrade gracefully in older browsers. However, it was a commercial product and I wanted something I could tweak myself. I thought to myself - "Guess I will just have to write my own.". So I did.

Highlights

When developing this control, I had several design goals in my mind:

Hopefully, when you download and try the attached code, you will find that all of these criteria have been met. The sample web page illustrates several scenarios in which the ComboBox control can be used. That said, I have not tried every single possible crazy scenario that could exist. If you find something that this control cannot do or suggestions for improvement, post your comments here and I will update the code.

Update - AutoFill

Several people asked me if this control supported the 'auto-fill' technique like that found at Google Suggest. I have added support for a simple version of this technique. By setting the EnableAutoFill property to true, the control will automatically fill in with the pre-defined values as the user types. (Unlike Google Suggest, however, the option list is not dynamically generated with each keystroke). Let me know how you like it.

Update - Design-time support and more

My bad on this one - I neglected to add the design-time attributes to the class files that I uploaded. This has been rectified, so now the control has full design-time support. Also, I fixed the issue that made the AutoFill case-sensitive. Please download the code again to get both of these fixes.

Update - .SelectedItem and .SelectedValue properties

Based on the suggestion and code sample from Pinndulum (see posts below), I have uploaded a new version of the control that better utilizes the .SelectedItem and .SelectedValue properties of the control. Now the text value and the value of the selected ListItem are both available programmatically. Thanks Pinndulum!

Update - SelectedIndexChanged not firing - fixed

When the control was set to AutoPostBack, the SelectedIndexChanged event was not being fired. This was due to some JavaScript that resets the selectedIndex property onchange. Although that is technically the correct behavior, having the SelectedIndexChanged event was more important, so I commented out the offending line. (Thanks to CGRothrock for finding this bug).

Update - Added support for 'GridLayout'

When the control was placed on pages that were using the GridLayout format, the control was not rendering in the correct spot. The bug was due to the fact that the designer-applied positioning was lost at render time. (Thanks to TWheelhouse for finding this bug and suggesting the fix).

Update - Added support for external JavaScript plus bug fixes

After several suggestions, I have finally added support for an external JavaScript file. If you set the ExternalResourcePath property to the location of a valid .js file, the control will use that instead of emitting the JavaScript inline. Also, I think the Firefox rendering/selectedIndex problem finally has an acceptable solution. Thanks to all who helped with ideas and fixes for this update.

Credits

My thanks goes to the authors of the other articles mentioned in this article for providing me the idea to create this control. Special credit must be given to the developers at Opinionated Geek for providing such a slick implementation that I had something to aspire to. Reviewing some of their JavaScript also provided valuable advice on how to get that pesky 1-pixel spacing issue resolved in IE browsers. =)

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
QuestionHow to disable input in combobox,just like a Dropdownlist? Pin
asatucap
23:26 1 Sep '09  
Generalthanks! Pin
spandal
18:28 29 Mar '09  
GeneralProblem entering the text Pin
mayureshp
2:24 2 Dec '08  
GeneralHow to multi-line on combobox? Pin
vac2507
23:06 24 Nov '08  
GeneralAdding items with Javascript Pin
rick2164
7:22 19 Nov '08  
GeneralTable border Pin
gecka
22:21 7 Jul '08  
Generalhi This control support master pages Pin
kirthigha
4:59 8 Feb '08  
QuestionTrying to access the control from the code behind. Pin
pskemp
9:02 25 Jun '07  
Generalset textbox value in codebehind Pin
dixbear
12:47 20 Jun '07  
QuestionAdding item using javascript Pin
cmb001
10:20 30 Mar '07  
GeneralIs dataset supported like datasource? Pin
andresfelipeclaros
12:42 12 Mar '07  
Generalasp.net 2.0 Pin
Sfinger
1:24 2 Mar '07  
GeneralRe: asp.net 2.0 Pin
xdyangyan
6:19 27 Mar '07  
GeneralHow to Fix the Height and width problem Pin
nji78
9:52 2 Nov '07  
GeneralRe: How to Fix the Height and width problem Pin
rwoehr
9:05 6 Feb '08  
GeneralRe: asp.net 2.0 [modified] Pin
Member 1216969
23:32 19 Jun '08  
QuestionHow do I get the new value the user typed in? Pin
Forogar
10:29 14 Dec '06  
QuestionStackOverflowException on postback [modified] Pin
nbrianc
17:40 4 Dec '06  
GeneralComboBox in GridView Pin
Wezz the Warlock
1:11 4 Dec '06  
QuestionHandwritten Why not? Pin
code.live
20:35 28 Nov '06  
GeneralComboBox Control in Array Pin
Seabert
20:59 14 Nov '06  
Generalerror message when control is hidden Pin
rock007
3:23 30 Oct '06  
QuestionAtlas Compatability Pin
bdk0172
10:47 10 Oct '06  
AnswerRe: Atlas Compatability Pin
MCofer
4:55 7 Feb '07  
GeneralRe: Atlas Compatability Pin
feitian78
1:46 21 Apr '09  


Last Updated 5 May 2006 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009