Click here to Skip to main content
Click here to Skip to main content

Custom ASP.NET Editable DropDownList

By , 15 Jan 2013
 

Introduction

The ASP.NET DropDownList is an easy way to offer a set of selections for the user. However, if you want to allow the user to enter information not available in the drop down list, you will have to write your own JavaScript code or jQuery. This source code combines that work into a ASP.NET DLL control you can easily place on any ASP.NET web page.

The downloadable source project above uses the following technologies:

  • ASP.NET 4.0 / Webforms
  • C#
  • jQuery / JavaScript
  • CSS

Example

Editable-DropDown/example1.jpg

Background

You do not have to have knowledge with jQuery, JavaScript, or CSS to use the EditableDropDownList. The only requirement is that any page that references the EditableDropDownList also contains the proper links to the CSS and JavaScript files available in either of the downloadable files above. The source code download contains the class library project for the EditableDropDownList and a test website using the EditableDropDownList control.

Using the Code

To use the EditableDropDownList, you will need to follow these steps:

  1. Include a project reference to the EditableDropDownList.dll to your ASP.NET webforms project.
    • You can skip steps 2 and 5 by using the EditableDropDownList from your Visual Studio toolbox.
    • To do so, be sure that you are currently editing a web page so that you can get access to the toolbox and ASP.NET controls.
    • From your Visual Studio toolbox, right click to create a new tab or use an existing one to add the EditableDropDownListControl. Start by right clicking in the toolbox and select "Choose Items.." and then browse to where you have saved the EditableDropDownList.dll.
    • Once added, you can now double click or click and drag the EditableDropDownList to your web page to get started. This will automatically add the reference to your web project, place the proper code in the header of your web page, and add the control into your web page.
  2. Register the EditableDropDownList at the top of your web page.
    <%@ Register Assembly="EditableDropDownList" 
    Namespace="EditableControls" TagPrefix="editable" %> 
  3. Add these files to your website:
    • jquery-ui.css
    • img/ui-bg_flat_0_aaaaaa_40x100.png
    • img/ui-icons_222222_256x240.png
    • img/ui-icons_454545_256x240.png
    • img/ui-icons_888888_256x240.png
    • (Javascript files below should be referenced in this order below)
    • jquery-1.6.4.min.js (* or greater)
    • jquery.ui.core.js
    • jquery.ui.widget.js
    • jquery.ui.button.js
    • jquery.ui.position.js
    • jquery.ui.autocomplete.js
    • jquery.ui.combobox.js (This file is not a part of the jQuery library)
  4. Add the references to the CSS and JavaScript files in the page header:
    <head runat="server">
        <title></title>
        <link href="css/jquery-ui.css" rel="stylesheet" type="text/css" />
        <script src="js/jquery-1.6.4.min.js" type="text/javascript"></script>
        <script src="js/jquery.ui.core.js" type="text/javascript"></script>
        <script src="js/jquery.ui.widget.js" type="text/javascript"></script>
        <script src="js/jquery.ui.button.js" type="text/javascript"></script>
        <script src="js/jquery.ui.position.js" type="text/javascript"></script>
        <script src="js/jquery.ui.autocomplete.js" type="text/javascript"></script> 
        <script src="js/jquery.ui.combobox.js" type="text/javascript"></script> 
    </head> 
  5. Add the script tags for the EditableDropDownList on your web page:
    <editable:EditableDropDownList ID="EditableDropDownList1" runat="server"> 
    </editable:EditableDropDownList> 
  6. Bind items to your drop down list either manually by adding ListItems to the EditableDropDownList or by binding to a DataSource.
    • Both examples can be found in the downloadable source code above.
  7. You're done! You can now work with your EditableDropDownList to find what options work best for you. I would love to hear some great feedback if you use this control on your site.

How is the EditableDropDownList Different from the Normal .NET DropDownList?

Here are the primary differences from the ASP.NET DropDownList control:

  • OnClick event is new.
    • You can now capture the event that a user clicked or selected an item. Capturing this event will cause an automatic page postback after the user makes a selection.
  • AutoWidth to fit the contents of the list items.
    • This functionality is a part of the browser and the <select> element. We are not using a select element for this control so it will be up to you to give a width that works well for the data that you are providing to the user.
  • Sorted [New/Added] (Boolean)
    • A new property "Sorted" has been added to allow you to automatically sort your items for you.
  • Duplicate entries will be ignored. If you have multiple entries provided in your drop down list, then only a single entry will be listed.
  • Blank dropdown list entries are not allowed. If you would like to provide a blank entry for the user to select, then I would suggest that you use "&nbsp;" as your display value.
  • If no dropdown list items are given or Enabled="false", then a simple text box will be displayed.
  • There may be some other properties that are available in the ASP.NET DropDownList like "DataTextFormatString" that will not work with the EditableDropDownList. If you would like to add this or any other functionality, you are welcome to modify the source code as needed.
  • The maximum dropdown height is set in the CSS file "css/jquery-ui.css" to 300px.
    • This will cause a scroll bar to always be visible but it will also prevent the drop down list from getting to big.
    • To turn this feature off, you will need to remove or comment out the line setting the max-height or you can adjust the maximum height.

Customization

If you would like to modify the look and feel of the EditableDropDownList, there are several ways of doing this:

  • Use CSS classes and set the CssClass property accordingly. (Be sure that the reference to your CSS file in the header is after the reference to the file "jquery-ui.css")
  • Set styles through the code behind of the page as you would with any ASP.NET control. (Example: EditableDropDownList1.Style.Add(HtmlTextWriterStyle.BackgroundColor, "#fefecc");)
  • Modify the jQuery CSS file "jquery-ui.css".
    • If you want to maintain a consistent look and feel throughout your website and everywhere that you use the EditableDropDownList, then this will be your best option.
    • Keep in mind that if you are using other jQuery UI controls, then changes will affect those controls as well.

History

  • 25th November, 2011: Article published
  • 15th January, 2012: Updated the EditableDropDownList control so that it will now work properly when used in a MasterPage
  • 25th February, 2012: Updated the EditableDropDownList control:
    • So that it will now work properly when used in a UpdatePanel
    • Allow AutoPostBack to be set to True
    • Added OnClick event
    • Added max-height the CSS file "css/jquery-ui.css" 
  • 18th June, 2012: Updated the  EditableDropDownList control: 
    • The original text input ID is set to the ClientID, the autocomplete text input ID is set to the ClientID + '_list', and the button is set to ClientID + '_list_button' so that these controls can be accessed in javascript.   
  • 19th July, 2012: Fixed a bug where the TextChanged and SelectedIndexChanged had stopped working properly due to the previous update. 
  • 12th November 2012: Moved the SelectedIndexChanged event to be raised just before the control's PreRender event so that all other controls on the page will have their data loaded and ready in-case they are needed. 
  • 19th November 2012: Fixed the following items. 
    • If the EditableDropDownList is disabled it will not lose it's text on postback.
    • TabIndex can be set.
    • If you call the Focus() method then jquery will be used to set focus on this control. 
  • 23rd November 2012: Added option to autoselect the first item of the dropdown list.
    • New boolean property name AutoselectFirstItem.
  • 19th December 2012: The drop down list will use the same font-family as its text box.
    • Added the style 'white-space: pre-wrap' to the dropdown list to allow for formatting with spaces.

Happy coding!  

License

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

About the Author

EricFaust
Web Developer National Research Corporation
United States United States
Began writing software at the age of 10.
Graduated from the University of Houston in 1995 with a BS in Computer Science and a minor in Math.
I love developing great software and websites.
I recently moved to Seattle and I'm currently working for a Health Care Analytics company.
 
Hobbies include Basketball and Soccer and very happily married to a wonderful wife.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHandling a newly inserted item [modified]memberBalakrishnans24-May-13 1:20 
Hi,
 
How to handle a newly inserted item?On which event will that item be captured?
Based on an item inserted I have to perform certain other tasks.But unable to capture the inserted item.
Regds,
Balakrishnan


modified 24-May-13 7:49am.

QuestionSelectedIndex property not available in javascriptmemberSantoshiChavan6-May-13 22:08 
First of all thank you so much for such a wonderful control.
 
I don't know what I am doing wrong. I am able to access the control through code behind. But when I try to access selected index through javascript it says 'undefined'. Any idea why this must be happening??
SantoshiKC

QuestionRangeValidator and the control cant be in the same page [modified]memberMember 992999117-Apr-13 21:05 
Thanks for the Control!
I got this problem in my page...
I have both the RangeValidator (to check other fields) and the Editable-Dropdownlist on my page
but I got the following error
"$(...).eddlcombobox is not a function"
 
seems that the problem exists to all ASP Validators

modified 18-Apr-13 3:50am.

QuestionTextChanged eventmemberMember 795726510-Apr-13 11:26 
Thanks for the control! It is awesome! But the problem I have is that if I enter a new value, the TextChanged event is not fired! Is that by design or am I missing something? I have AutoPostBack set to true but that works if I pick up an existing value from the dropdown.
QuestionHow to disable ans enable this Editable dropdown control using javascriptmembershashikumarpathi8-Apr-13 18:30 
I need to know that how can i disable and enable this control using java-script.
sir, i am waiting for your positive response.
QuestionDynamic Databound doesnt work, Pls helpmemberjshrilekha2-Apr-13 20:24 
Hi,
I'm trying to implement this custom control to Gridview Template column. On postback the SelectedValue is always "" . Can you please help me to overcome this issue?
 
aspx:
<asp:GridView ID="gvRoomTypes" runat="server" OnRowDeleting="gvRoomTypes_RowDeleting"
Style="text-align: left" OnRowDataBound = "gvRoomTypes_RowDataBound">

<asp:TemplateField HeaderText="Type Char" HeaderStyle-CssClass="ContentLabelBoldWhite">
<editable:EditableDropDownList ID="TypeChar" runat="server" Sorted="true" Text='<%# DataBinder.Eval(Container.DataItem,"Typechar") %>'>



 
Code
EditableControls.EditableDropDownList txtTypeChar = (EditableControls.EditableDropDownList)gvRoomTypes.Rows[rowIndex].Cells[2].FindControl("TypeChar");
 
dtRoomTypeDetails.Rows[i - 1]["TypeChar"] = txtTypeChar.SelectedValue;
AnswerRe: Dynamic Databound doesnt work, Pls helpmemberjankokv4-Apr-13 5:17 
Try to put ClientIDMode="Static" in EditableDropDownList.
Regrads,
Janko
QuestionAuto open dropdownmemberphlat15-Mar-13 11:27 
Hi. This is a nice control. My question is once the drop down has been populated how would I auto open the drop down without having to click the bitmap button? Thanks!!
GeneralMy vote of 4memberabhi99314-Mar-13 7:10 
it contains good example
Questionstyle for dropdownlist part of editabledropdownlistmemberpra44445-Mar-13 12:00 
My page has a light gray background. Since the dropdown image of the editable dropdownlist is almost grayish, its not clearly visible as one would like.
How can i change the css to a different color?
 
Thanks.
QuestionCustom ASP.NET Editable DropDownList not passing value in javascriptmemberItagiGuru27-Feb-13 19:41 
Hi, Custom ASP.NET Editable DropDownList Loading values but i am not able to select values from javascript
For Ex:
var jsontext = JSON.stringify({ p_Vcode: $("editable [id$='ddl_Village'] option:selected").val(), p_ycode: $("select[id$='ddl_Season'] option:selected").val()})
 
in this p_Vcode not getting values from editabledropdown list.
SuggestionDown loadmembersaguntamrakar17-Feb-13 17:38 
Source code is not downloading of editable dropdownlist so please try
to rectify that error::
QuestionProblem with drop down list post back in updatepanelmemberVitali Unke1-Feb-13 7:28 
I noticed one issue,
 
When I put the drop down list in an update panel and make post back which causes changing on other control in other update panel. If second panel is set to update mode Conditional and set AsyncPostBackTrigger for drop down list from first panel, changes are not visible.
 
If I set the second panel update mode to always the changes are visible.
QuestionHow to setting the style of list box of editabledropdownlist.members843210520-Jan-13 16:16 
Hello
 
When I set the font-size property of the component, it's effected only the appearance of 'textbox' part of the component but the 'list' part still be render the same way before the component was edited font-size style.
 
How can I set the style of 'list' part of editable dropdownlist?
 
Thank you.
AnswerRe: How to setting the style of list box of editabledropdownlist.memberEricFaust23-Jan-13 21:33 
If you want to change the style of all dropdown lists then you can edit the css style for '.ui-menu-item'.
GeneralRe: How to setting the style of list box of editabledropdownlist.members843210523-Jan-13 22:11 
Thank you.
GeneralRe: How to setting the style of list box of editabledropdownlist.memberkuldeep@1234515-Mar-13 0:54 
hey Eric, if dorpdown list doesnt find any match in record, it will allow user to type data though there is no match but i want if no match found in dorpdownlist's record i want to select the first record by default.... what to do for this kind of functionality????????
GeneralRe: How to setting the style of list box of editabledropdownlist.memberEricFaust17-Mar-13 11:50 
Hello kuldeep, you will have to either write code server side or client side. So that after the user enters their value if it does not match an item in the list this it will default as you mentioned. There may be other dropdown lists that offer the functionality that you are looking for but this one is not setup to work that way. Thanks.
GeneralRe: How to setting the style of list box of editabledropdownlist.memberkuldeep@1234517-Mar-13 19:39 
thank you Eric, I will try it by code....
QuestionList box cannot select defualt value with ListItem selectedmemberVitali Unke18-Jan-13 12:00 
When I bind the dropbox with list of objects List
and one ListItem property Selected is true, the combox dont select that value.
Where am I wrong?
AnswerRe: List box cannot select defualt value with ListItem selectedmemberEricFaust19-Jan-13 11:28 
Hi Vitali,
I had to remove the ability to select an item by setting selected as true in the html listbox code because by default the first item is always true in a listbox control and so I wouldn't know if it was supposed to be selected or not. So what you will have to do is one of several options. You can do these from the control or in the code behind.
1. Set the SelectedIndex property.
2. Set the SelectedValue property.
3. Set the SelectedItem property.
4. Set the Text property.
 
Let me know if you need more help.
GeneralMy vote of 5memberMember 969271917-Jan-13 7:17 
Helpful
QuestionHi, Need to add :focus property to CSSmemberanupmbaibm17-Jan-13 1:14 
Hi, Ur control works just great for us. However, we need to set a custom border to your DDL when this comes to focus. In a regular ddl, suppose we are using a class .dropdown in our CSS, we would just write a .dropdown:focus method. Where do I set such a property in your CSS?
 
We are trying to use .ui:focus { border: 2px solid red; } at the end of your jquery-ui.css but this does not seem to give us the effect.
AnswerRe: Hi, Need to add :focus property to CSSmemberEricFaust17-Jan-13 22:08 
Hello,
the way that the control works is that all of its styles are stored in-line so you will need to add jquery to replace the styles that you want to update. here is some example code:
<script type="text/javascript">
    $(document).ready(function () {
        $('.ui-autocomplete-input').removeAttr('style');
        $('.ui-autocomplete-input').addClass('ui-widget');
        $('.ui-autocomplete-input').addClass('ui');
    });
</script>

QuestionCannot input text when control is added to TabContainermemberVitali Unke16-Jan-13 9:31 
Once I add this control into TabContainer I cannot input text. the Dropdown button works.
 
Any Ideas?
 
thank you.
 
By the way great control. thank you.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 16 Jan 2013
Article Copyright 2011 by EricFaust
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid