Click here to Skip to main content
15,897,291 members
Articles / Web Development / ASP.NET

Create a ComboBox web control with jQuery and ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.38/5 (5 votes)
4 Jun 2012CPOL5 min read 65.3K   2.6K   22  
ComboBox is a common used control that has no default support by HTML specification and ASP.NET. In this article, I will present a ComboBox web control that is built with jQuery and ASP.NET.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;

[assembly: TagPrefix("Demo", "Sample")]
namespace Demo.WebControls
{
    /// <summary>
    /// ComboBox web control
    /// </summary>
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:ComboBox runat=\"server\"></{0}:ComboBox>")]
    public class ComboBox : TextBox
    {
        private DropDownList _ddl; //this is used to keep ComboBox items in HTML

        /// <summary>
        /// DataSource
        /// </summary>
        [Category("Data")]
        public object DataSource
        {
            get
            {
                EnsureChildControls();
                return _ddl.DataSource;
            }
            set
            {
                EnsureChildControls();
                _ddl.DataSource = value;
            }
        }

        /// <summary>
        /// DataSource ID
        /// </summary>
        [Category("Data")]
        public string DataSourceID
        {
            get
            {
                EnsureChildControls();
                return _ddl.DataSourceID;
            }
            set
            {
                EnsureChildControls();
                _ddl.DataSourceID = value;
            }
        }

        /// <summary>
        /// DataMember
        /// </summary>
        [Category("Data")]
        public string DataMember
        {
            get
            {
                EnsureChildControls();
                return _ddl.DataMember;
            }
            set
            {
                EnsureChildControls();
                _ddl.DataMember = value;
            }
        }

        /// <summary>
        /// DataTextField
        /// </summary>
        [Category("Data")]
        public string DataTextField
        {
            get
            {
                EnsureChildControls();
                return _ddl.DataTextField;
            }
            set
            {
                EnsureChildControls();
                _ddl.DataTextField = value;
            }
        }

        /// <summary>
        /// DataTextField
        /// </summary>
        [Category("Data")]
        public string DataValueField
        {
            get
            {
                EnsureChildControls();
                return _ddl.DataValueField;
            }
            set
            {
                EnsureChildControls();
                _ddl.DataValueField = value;
            }
        }

        /// <summary>
        /// Items
        /// </summary>
        [
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
        PersistenceMode(PersistenceMode.InnerDefaultProperty)
        ]
        public ListItemCollection Items
        {
            get
            {
                EnsureChildControls();
                return _ddl.Items;
            }
        }

        /// <summary>
        /// data bind
        /// </summary>
        public override void DataBind()
        {
            _ddl.DataBind();
        }

        /// <summary>
        /// create child controls
        /// </summary>
        protected override void CreateChildControls()
        {
            _ddl = new DropDownList();
            _ddl.TabIndex = -1;
            _ddl.Style.Add("display", "none"); //DropDownList is hidden and used as datasource in HTML markup of ComboBox
            Controls.Add(_ddl);

            base.CreateChildControls();
        }

        /// <summary>
        /// On Pre Render
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            Page.ClientScript.RegisterClientScriptResource(typeof(ComboBox), "Demo.WebControls.Scripts.ComboBox.js");
        }

        /// <summary>
        /// Render
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            EnsureChildControls();

            writer.Write("<span style=\"position:relative;\" ComboBox=\"1\" >");
            base.Render(writer);
            writer.Write("<input type=\"button\" value=\"V\" />");
            _ddl.RenderControl(writer);
            writer.Write("<div style=\"visibility:hidden; background-color:white\"></div>");
            writer.Write("</span>");
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior)
United States United States
Senior Software Developer from New Jersey, USA

Have 15+ years experience on enterprise application development with various technologies.

Comments and Discussions