Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Dropdown TextBox

0.00/5 (No votes)
31 Jan 2012 1  
Custom TextBox with a dropdown list

Introduction

If you are a .Net developer, you may notice that there is an important difference (in terms of features) between the WinForm ComboBox and the WebForm DropDownList. ComboBox of WinForm allows user to input custom item if the user cannot find the item in the combo list. However, this feature is omitted in the DropDownList of WebForm. In order to make WebForm DropDownList as flexible as WinForm ComboBox, I decide to build a DropdownTextBox for WebForm with the feature similar to the WinForm ComboBox.

Using the code

You can download the zip file above and use it immediately. The zip file contains a demo web page and source code. As the code is quite straight forward, I do not want to write a lengthy explanation here. If you want to see a live demo before you download it, you can go to my other blog.

Essentially, the DropdownTextBox is built from the combination of a TextBox, a ListBox and a PopupControlExtender of the AjaxControlToolkit. The demo uses AjaxControlToolkit for .Net4.0 but AjaxControlToolkit for .Net3.5 also works correctly. In my demo project, the control IDs are TextBox1, ListBox1 and PopupControlExtender1. In order to make the DropdownTextBox works, you also need to add the following codebehind:

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        TextBox1.Text = ListBox1.SelectedValue
        Me.PopupControlExtender1.Commit(ListBox1.SelectedValue)
    End Sub

Points of Interest

Both TextBox and DropDownList are very useful in ASP.Net development. This DropdownTextBox is a combination of the two useful controls so that user can either type in custom item or select an item from the dropdown list.

History

This is the first version.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here