ASP.NET AJAX ComboBox

by mosessaur| 06 January 2008| 33 Comments

Back to April 2007 I posted about building an ASP.NET AJAX ComboBox. It was an attempt to rewrite a ComboBox Control I made to fulfil certain issues I faced with 3rd Party Controls. One of those issue was performance as most of 3rd Party control are rendering heavy HTML because of extinsive features they put on their controls which I do not need. So This post is supposed to be an extensions to my old post as well as my article about building Simple ASP.NET ComboBox.

Attached to this post is the ASP.NET AJAX ComboBox. The archive is also containing 3 other very simple controls. The controls are developed using ASP.NET AJAX 1.0. Client Side Controls are written using Script#. I've posted earlier about Script# and my experience with it, so feel free to have a look at that post.

Now back to ASP.NET AJAX ComboBox. The control is very simple, you can consider it as composite control of simple ListBox and TextBox, however it doesn't inherit from CompositeControl. It directly inherit from ListControl to support binding out of the box and many other features that do need any to be rewritten.

How to use this simple control? The control has a property called EditingStyle. The property accepts one of the following values:

  • DropDownList: Pretty similar to normal DropDownList behavior.
  • ComboBox: TextBox is enabled for free text editing. No auto complete is offered.
  • DropDownCombo: TextBox is enabled for free text editing, Auto complete is offered for the available list items.

For the look and feel of the control there are few CSS style properties described bellow:

  • CssClass: Normal CSS class for the TextBox.
  • HoverCssClass: Hover CSS class for the TextBox when mouse goes over it.
  • FocusCssClass:Focus CSS class for the TextBox when recieving input focus.
  • ReadOnlyCssClass: ReadOnly CSS class, used when the control is in read only state. Note that ReadOnly is different from Enabled property.

I did not expose any client side method to public, actually because there are no needed client side methods so far. But there are few properties are exposed as public, I did that for development time use, but never face a case that I needed to use them from client code. These properites represents the DOM Elements of:

  • Text box -> get_textBox()
  • Drop down image -> get_dropDownImage()
  • List control -> get_listBox()

Also there are some useful properties exposed that represents selected item Index,value and text:

  • get_selectedIndex() returns integer
  • set_selectedIndex(ndx) accepts integer parameter
  • get_value() returns selected item value
  • get_text() returns text of the TextBox

The control expose only one event to develpers. This event is change event of the List Box. The client function name used to handle this event should be set to OnClientChange property of the control. This function should have the following signature

function onClientChange(s,e) {.....}

s parameter is the sender onject, it always hold a reference to the control itself.
e parameter is an EventArgs argument, it is always empty not used in other words currenlty.

So if you wish to access curently selected index of the comboBox you could something similar to this:

   1: function ProductChanged(s,e){ 
   2: alert(s.get_selectedIndex()); 
   3: } 

Now the following is code sample of how to declare the ComboBox.

   1: <cc1:combobox id="ComboBox1" 
   2: EditingStyle="DropDownList" CssClass="textbox" 
   3: HoverCssClass="textboxhover" ReadOnlyCssClass="textboxreadonly" 
   4: FocusCssClass="textboxfocus" OnClientChange="ProductChanged" 
   5: datasourceid="SqlDataSource1" datatextfield="ProductName" datavaluefield="ProductId" runat="server" /> 

.....

   1: <scripttype="text/javascript">
   2:  function ProductChanged(s,e)
   3:  {
   4:      alert(s.get_selectedIndex());
   5:  }
6:</script>

There is an issue with this control, that it is missing good design time support so sorry for this, sorry for this!

I think this is all I have in mind now, if I found something important to be mentioned I'm gonna posted as soon as I can. Thanks for all those who supported me by suggestions and testing in my previous version. And sorry if the old one wasn't that much satisfying! hopefully this one would be better.

UPDATE:

  1. BUG 01 09 Jan 2008:When placing the control inside a container (DIV, Fieldset etc....) and set positioning to this container, the List Box is not displayed in its approperiate location.
  2. BUG 01 FIX 12 Jan 2008: Fixed normally on Opera and IE. FireFox is also fixed with workaround that I cannot guarantee 100% that it is totally fixed. For some reason FF do not calculate position the way IE and Opera do:
    Here is how IE and Opera fix work:
       1: var textBoxBounds = Sys.UI.DomElement.getBounds(this._textBox);
       2: var offsetParent = Sys.UI.DomElement.getLocation(this._textBox.offsetParent);
       3: var x = textBoxBounds.x - (offsetParent.x + 2); //Added extar 2 pix to make the list display exactly underneath the textBox 
       4: var y = (textBoxBounds.y + textBoxBounds.height) - (offsetParent.y + 1); //Same thing as above 
       5: Sys.UI.DomElement.setLocation(this._listBoxContainer, x, y);
  3. The above code work perfectly in both IE and Opera but not in FF for some reasons I don't know! So I had to write similar code but I had to increase those extra pixels extra ordinary like this
       1: var x = textBoxBounds.x - (offsetParent.x + 12); //Added extar 2 pix to make the list display exactly underneath the textBox
       2: var y = (textBoxBounds.y + textBoxBounds.height) - (offsetParent.y + 27); //Same thing as above
  4. I've tested it and it is working properly, but my test was very fast. Hopfully this bug is fixed. Download attached Project as it contains the fix.
  5. Bug 02, fixing Bug 01 caused a bug when placing ComboBox within GridView. The list is not positioned correctly under the TextBox, and actually it flies away.
  6. Bug 02, FIX . 6th Feb 2008.
  7. New version 1.0.2.1 24th Feb 2008. Support for Tab and Enter key selection. Resolve an issue when only one item is in the list and this item is selected. You type something in TextBox and still that item is selected, now it is being deselect.

SCS.Web.Controls.AJAXv1.1.20.zip (65.55 kb)
SCS.Web.Controls.AJAXv1.0.21.rar (51.87 kb)

kick it on DotNetKicks.com

Comments (33) -

wilk
wilk United States on 2/7/2008 8:59 AM Mosessaur -

I have been experimenting with web comboboxes, and this is one of the best that I have found.  I wanted to create a javascript to handle the event when the user manually enters text.  I see that you expose a onTextChanged event, but it does not appear to a client-side event.  Is there a client-side way to handle the event where a user manually enters a value?
mosessaur
mosessaur Egypt on 2/8/2008 7:38 PM Dear Wilk, actually I didn't thought of exposing TextBox client change event in the Client Side APIs.
But thank you for the hint, I'll try to add this feature.
However as the source code included, you can try to add this feature if you are in a hurry. It can be added just as Change client event. But you'll need to use Script# for that.
Anyway, I'll try to look at in the weekend, I also has couple of bugs to try to resolve.
Thank you for the hint
johny
johny United States on 3/4/2008 3:52 PM i can't convert your solution into web developer 2008/3.5.
can you please provide the dll? it's not in your zip.

thanks
mosessaur
mosessaur Egypt on 3/5/2008 7:30 AM As I am not on my machine now, I cannot recompile and upload the dll, but you can go to projects.nikhilk.net/ScriptSharp/Default.aspx and download VS.NET 2008 version of Script# I guess this should work.
Regards
drew
drew United States on 3/19/2008 11:19 AM great control! found a display bug, though: in IE6, if there is a normal asp:dropdownlist below this combo box, when you type something into the textbox of this control, the popup list box with the databound items shows up behind the normal dropdownlist. clicking on the arrow image renders the listbox correctly on top of other controls. it also displays correctly in ie7 and firefox regardless of how it is used.

here's a screenshot of the issue: i273.photobucket.com/.../comboboxissue.jpg

any ideas?
mosessaur
mosessaur Egypt on 3/19/2008 9:05 PM Foot Really! That is a weird bug or weird browser IE 6, but really good catch! To be honest, I'll try to look at it, and fix it, but might not be able to test. I guess I just need to set the z-index in the Script# code and regenerate the JavaScript file and test it.
Kontax
Kontax Italy on 3/26/2008 10:59 AM Hi muhammad,
this control seems great, and i'd really like to try it, but i've only Visual Web Developer and counldn't manage to use script#...Do you think you can ever upload the .ddl?
Thanks anyway, bye
mosessaur
mosessaur Egypt on 3/26/2008 10:24 PM Ok, let me give you a hint and then I'll manage to upload the DLL.
Do you have Visual C# Express?! if you do, compile the control project.
Then add it as a reference into your Visual Web Developer Project.
And give me 2 days or something and I'll upload a compiled version of it.
Sorry it is just I am away from the machine that has VS.NET Installed on it, as well as I'll be away for 2 days. Thank you
Kontax
Kontax Italy on 3/27/2008 12:37 PM Thanks!
I've got C# Express and compiled the control project, then added it into the project without problem, easier than i thought.
But, i have a stupid problem..As you could have understood it's few days I use Visual Developer..so i can't understand how to add a ComboBox after I added the reference into the project..it's not possible to declare it as <cc1:combobox >..
Can you tell me how to add it to the toolbox, or use it in the C# code (tried to add the  SCS.Controls namespace but still I don't understand the exact syntax..)?
Thanks anyway for the hints you gave me, will try to get it work ayway..
Kontax
Kontax Italy on 3/28/2008 6:13 AM Thanks!
I downloaded C# Express and compiled the control project, easier than I thought..
Just one more question..after that, I was still unable to use the controller cause I couldn't declare it with <cc1:combobox> or from the c# code (tried to add the namespace but still don't know the exact syntax)..
Can you suggest me what I have to do next?
Thanks anyway for the reply and for the hints you gave me,
Bye!
Cepes
Cepes Italy on 3/28/2008 4:42 PM Hi Muhammad I had the same problem of the previous guy ("Kontax"). I tryied to follow your hints, but after I added the reference I still couldn't use the control. How can I add it in the toolbox?
Thanks for your attention...
mosessaur
mosessaur Egypt on 3/31/2008 2:27 AM Sorry, I was away for 2 days, Friday and Saturday!
Ok, all you need to do, is to compile the Control Project. Then choose to add reference in the Web Site, and browse for the compiled DLL.
In Web.Config you can do the following to register the control to be available in all  pages:
<pages>
  <controls>
    <add assembly="SCS.WebControls" namespace="SCS.WebControls" tagPrefix="scs" />
  </controls>
</pages>

Regarding Toolbox, all you need to do is to open your toolbox, right click on it and choose to add me items. It will open to you a window where you can browse for the compiled DLL. Choose it and it should add the control to the toolbox.

Note: The control really has bad design time support, I recommend to use the ASPX code instead.

Heinrich Mueller
Heinrich Mueller Germany on 4/2/2008 3:22 PM The fixes for Internet Explorer from 12 Jan 2008 do not work. The list do not display exactly underneath the Textbox. How can I or you fix it?
mosessaur
mosessaur Egypt on 4/2/2008 4:42 PM @Heinrich,
I've downloaded the code from this post. And tested it with IE 7 and it working just fine. IE 6 Foot is not supported as I never test on it.
Check the sample "working/WebForm1.aspx" on the attached project.
Kontax
Kontax Italy on 4/6/2008 5:44 AM Ok, thanks, I could try your control..
And it's great!
I tried also some commercial ComboBox, such as the Telerik one, and I have to say that for a not intense use yours is definitely better..
Thanks for everything!
Bye
mosessaur
mosessaur Egypt on 4/6/2008 5:54 AM Thank you Kontax, really I wish to make it more better. I just need time to resume working on it, hopefully soon.
Kindly feel free to submit any bugs or send me any updates to made.
Kontax
Kontax Italy on 4/7/2008 8:37 AM hi,
sorry, it's me another time..
I encountered a problem with the control, maybe you'd like to know:
if I place it in the Tab control of the Ajax Control toolkit it doesn't work (doesn't display the list).
I tested it, with both the versions, into FF and IE and it happens every time, I think it's not my fault.
Thanks, bye
mosessaur
mosessaur Egypt on 4/7/2008 8:41 AM I would give it a try and see what is wrong, hopefully will be able to get back to you ASAP. I never tested with AJAX Control Toolkit controls.
Thank you for letting me know, and you are welcome anytime.
Greg Brotzge
Greg Brotzge United States on 4/26/2008 4:03 AM hi there,
Thanks for the control, it's the best free combo box I've been able to find.  It's coming in handy the way it is, but I've found a couple issues I'd love to see improved in future versions.
  
This is probably the same issue as Kontax's last post.. If I place the combobox control in an AJAX ModalPopupExtender panel then the dropdown window on the combo does not appear at all.

Another, but very minor issue I've found, in Firefox the dropdown arrow image appears slightly lower than in IE.  I understand if there's just no way to fix this, I think I've run into the same issue in another case before, but it sure would be great to find a fix..

Thanks!
mosessaur
mosessaur Egypt on 4/26/2008 3:10 PM Thank you Greg, actually first time to be aware of the issue with ModalPopupExtender panel. I need to test it, I will try to do that soon, I'm actually loaded these days!

I know about the drop down image issue with FF. I'm sure that there are some work around or resolution to it, but I don't know how! if you could reach to something please let me know.

thank you
chester..
chester.. United States on 5/20/2008 10:09 AM css textboxt ınput (textfield) style - examples - -
www.css-lessons.ucoz.com/textbox-css-examples.htm
mosessaur
mosessaur Egypt on 5/21/2008 1:21 AM Thank you for the link Chester
bryian
bryian United States on 5/26/2008 12:57 PM where to see the demo? thanks
mosessaur
mosessaur Egypt on 5/28/2008 7:51 AM So far I didn't put on any demos, but I should think about it and try to put some on soon.
Jerrol
Jerrol United States on 10/22/2008 4:53 AM What is the purpose (and how is it created?) of the ClientControls folder in the download?

Your code is great but I need to not display the dropdownlist when the user "Tabs" out of the dropdownlist.  I've not found in your code where you reference the "Enter" button (which works fine and I would like to copy for Tab.)

Can you suggest how I would go about this?  I did see a function onKeyPress in the ClientControls\ComboBox.cs file but changes I made didn't appear to be used when I tested the results.

Thanks,

Jerrold
mosessaur
mosessaur Egypt on 10/22/2008 5:07 AM @Jerrol I need to look at it again, long time I didn't touch this code. I thought I ready made that tab key option feature. It need sometime, sorry for this enconveniance.
Jerrold
Jerrold United States on 10/22/2008 6:20 AM I forgot to mention it works with the enter key because it does a postback, I had the autopostback set to false when the dropdownlist continues to show.  I have to do this since I have a large cached list.

Is the onKeyPress and trap for Tab and Enter key the way to correct this?  I'd be glad to send you the code if you could suggest how I would go about revising it.  

Or it would be great if you can get to it.

Thanks again for the great control with the speed I need!

Jerrold

Thamilarasi
Thamilarasi India on 12/24/2008 1:11 AM I want to increase the asp drop down list height. But it not supported in Interner Explorer 6, when I gave the syntax like this

<asp:dropdownlist height="100px"></as:dropdownlist> (or)

css
.cbobox{height:100px;}

<asp:dropdownlist cssclass="cbobox"></as:dropdownlist>

pls help me to solve this problem.
ramesh nagineni
ramesh nagineni India on 12/28/2008 3:04 PM Hi
hoW to set the width and hight for comboBox
is it possibe for this control....

Please help me Urgent....

Thanks
ramesh
mosessaur
mosessaur Egypt on 12/29/2008 1:18 PM @ramesh nagineni Did try to set the height but the width should work
Corey
Corey United States on 1/20/2009 2:24 AM Hi,

Any update on the issue with the ModalPopupExtender? Your control works great in a regular page, but doesn't display the ListBox at all when placed inside a ModalPopupExtender. Thanks.
dan
dan United States on 7/22/2009 7:19 AM Muhammad,

Hello. I have successfully added an ASP.NET FilteredTextBoxExtender within your combobox

adding

        AjaxControlToolkit.FilteredTextBoxExtender _ftbex;

to your Combobox.cs Private Members region, then within CreateChildControls()

            _ftbex = new FilteredTextBoxExtender();
            _ftbex.ID = "filtTBEx";

            _ftbex.FilterType = FilterTypes.LowercaseLetters | FilterTypes.UppercaseLetters | FilterTypes.Numbers | FilterTypes.Custom;
            _ftbex.ValidChars = "'`./-&, ";
            _ftbex.TargetControlID = "TB";
.
.
.
            this.Controls.Add(_ftbex);

then in Render()

            _txtBox.RenderControl(writer);
is followed by
            _ftbex.RenderControl(writer);

That actually works, and thank you for the compatibility of your source!

However, when I place the SCS.Web.Controls.ComboBox in a Scrollable ASP.NET Panel control, and scroll down to it, then when I click on the combobox the list appears inches below the panel area rather than right underneath the combobox. I need to do the same thing with an ASP.NET 3.5 ListView as well. Possibly you might be able to provide a recipe to avoid the problem, or a fix for that? Or, alternatively, do you have a combobox that adds the ability to type a value not listed in the dropdown, and which allows filtering out unsecure inputs (such as '<', '>') that you'd recommmend? (The currently released ASP.NET Ajax Control Toolkit ComboBox appears to have similar issues btw).

Anyway, great work, and thank you!
mosessaur
mosessaur Egypt on 7/23/2009 7:49 AM I wrote this control long time ago! I'm really not able to follow up with my own code Frown
I had a plan to rewrite it using jQuery but couldn't allocate the time. sorry for the poor support but really I am not able to allocate time to invistigate this control issues.
Thank you for the comment
Comments are closed