Click here to Skip to main content
6,822,123 members and growing! (16,343 online)
Email Password   helpLost your password?
Web Development » Custom Controls » General     Intermediate

Custom ComboBox server control for ASP.NET

By bestcomy

A custom ComboBox control for ASP.NET.
C#, Windows, .NET1.1, ASP.NET, WebForms, VS.NET2003, IE6.0, Dev
Posted:11 Apr 2005
Updated:6 May 2005
Views:135,303
Bookmarked:40 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
19 votes for this article.
Popularity: 4.65 Rating: 3.63 out of 5
2 votes, 10.5%
1
4 votes, 21.1%
2
2 votes, 10.5%
3
2 votes, 10.5%
4
9 votes, 47.4%
5

Sample Image

Introduction

In my work, I needed a ComboBox Server Control, and there wasn't one with the specifications I needed. After much searching and trying, none of them satisfied my requirement, I need one which could behave like a dropdownlist absolutely and could get an input value. But I got a HTML combobox control from here, which was what I was looking for. So I developed a ComboBox Server Control based on this HTML combobox control and decided to share it with all.

Background

The HTML component is built by using one textbox and one Select, the Select is dynamically repositioned at runtime. My work encapsulates the HTML component as an ASP.NET server control and you can use it like a typical listcontrol in ASP.NET.

Using the code

You can use this control in three ways as follows:

for(int i=1; i < 10; i++)
{
    ComboItem item = new ComboItem("Item"+i.ToString());
    ComboBox1.Items.Add(item);
}
<bestcomy:ComboBox id="ComboBox2" runat="server" Width="120px">
    <BESTCOMY:COMBOITEM Text="Item1"></BESTCOMY:COMBOITEM>
    <BESTCOMY:COMBOITEM Text="Item2"></BESTCOMY:COMBOITEM>
    <BESTCOMY:COMBOITEM Text="Item3" Selected="true"></BESTCOMY:COMBOITEM>
    <BESTCOMY:COMBOITEM Text="Item4"></BESTCOMY:COMBOITEM>
</bestcomy:ComboBox>
DataTable dt = new DataTable();
dt.Columns.Add("text", typeof(string));
for(int i=1; i < 10; i++)
{
    DataRow ndr = dt.NewRow();
    ndr["text"] = "Item" + i.ToString();
    dt.Rows.Add(ndr);
}

ComboBox3.DataSource = dt.DefaultView;
ComboBox3.DataTextField = "text";
ComboBox3.DataBind();

Points of Interest

Keeping viewstate in the round trip is really interesting. You can see how I achieved this, in the following script:

ComboBox.cs

        protected override void TrackViewState()
        {
            base.TrackViewState ();
            ((IStateManager)this.Items).TrackViewState();
        }

        protected override object SaveViewState()
        {
            object obj1 = base.SaveViewState();
            object obj2 = ((IStateManager)this.Items).SaveViewState();
            object obj3 = this.Text;
            if(obj1==null && obj2==null && obj3==null)
                return null;
            return new Triplet(obj1,obj2,obj3);
        }

        protected override void LoadViewState(object savedState)
        {
            if(savedState!=null)
            {
                Triplet state = (Triplet)savedState;
                base.LoadViewState(state.First);
                ((IStateManager)this.Items).LoadViewState(state.Second);
                _text = (string)state.Third;
            }
        }

ComboItemCollection.cs

        public void TrackViewState()
        {
            this._IsTrackingViewState = true;
            for(int i=0; i < this._items.Count; i++)
            {
                ((IStateManager)this[i]).TrackViewState();
            }
        }

        public bool IsTrackingViewState
        {
            get
            {
                return this._IsTrackingViewState;
            }
        }

        public object SaveViewState()
        {
            ArrayList list1 = new ArrayList();
            ArrayList list2 = new ArrayList();
            for (int num3 = 0; num3 < this.Count; num3++)
            {
                object obj1 = ((IStateManager)this[num3]).SaveViewState();
                if (obj1 != null)
                {
                    list1.Add(num3);
                    list2.Add(obj1);
                }
            }
            if (list1.Count > 0)
            {
                return new Pair(list1, list2);
            }
            return null;
        }

        public void LoadViewState(object state)
        {
            if (state == null)
            {
                return;
            }
            if (state is Pair)
            {
                Pair pair1 = (Pair) state;
                ArrayList list1 = (ArrayList) pair1.First;
                ArrayList list2 = (ArrayList) pair1.Second;
                for (int num1 = 0; num1 < list1.Count; num1++)
                {
                    int num2 = (int) list1[num1];
                    if (num2 < this.Count)
                    {
                        ((IStateManager)this[num2]).LoadViewState(list2[num1]);
                    }
                    else
                    {
                        ComboItem item1 = new ComboItem();
                        ((IStateManager)item1).LoadViewState(list2[num1]);
                        this.Add(item1);
                    }
                }
            }
        }

ComboItem.cs

        public void TrackViewState()
        {
            this._IsTrackingViewState = true;
        }

        public bool IsTrackingViewState
        {
            get
            {
                return this._IsTrackingViewState;
            }
        }

        public object SaveViewState()
        {
            return new Pair(this._text,this._selected);
        }

        public void LoadViewState(object state)
        {
            if(state!=null && state is Pair)
            {
                Pair p = (Pair)state;
                this._text = (string)p.First;
                this._selected = (bool)p.Second;
            }
        }

History

  • 28 April, 2005 - Design time support for Items.
  • 7 April, 2005 - First release.

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

About the Author

bestcomy


Member

Occupation: Web Developer
Location: China China

Other popular Custom Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 36 (Total in Forum: 36) (Refresh)FirstPrevNext
GeneralLicense PinmemberHelen Siver5:27 25 May '09  
GeneralCombobox not rendering items properly Pinmemberwdycee217:36 26 Aug '08  
GeneralRe: Combobox not rendering items properly Pinmemberbestcomy19:22 26 Aug '08  
GeneralRe: Combobox not rendering items properly Pinmemberwdycee216:52 27 Aug '08  
GeneralCombo box not loading Pinmemberceetee4:42 5 May '08  
GeneralASP.NET 2.0 Pinmembermoskis275:22 16 May '07  
GeneralHow do you support custom prefixes with the options? Pinmemberhowardjr18:35 29 Nov '06  
GeneralRe: How do you support custom prefixes with the options? Pinmemberbestcomy20:25 29 Nov '06  
GeneralRe: How do you support custom prefixes with the options? Pinmemberhowardjr20:53 29 Nov '06  
GeneralRe: How do you support custom prefixes with the options? Pinmemberhowardjr13:12 1 Dec '06  
Generalgood job ,i like it.. Pinmemberwinart21:32 11 Sep '06  
GeneralMuch quicker approach PinmemberMartin Auve6:30 12 Apr '06  
GeneralRe: Much quicker approach PinmemberDark-ranger1:38 13 Jun '06  
GeneralRe: Much quicker approach PinmemberMartin Auve10:52 12 Jul '06  
GeneralRe: Much quicker approach PinmemberMartin Auve10:54 12 Jul '06  
GeneralSelected Index change PinmemberThoths0:34 8 Nov '05  
GeneralRe: Selected Index change PinmemberKrishnaDwivedi20:33 4 Jul '06  
Generalhow add row to a combobox PinsussAnonymous1:39 10 Oct '05  
GeneralComboBox WIDTHS!! PinmemberDjIndy234:16 5 Aug '05  
Generalhttp://www.metabuilders.com/Tools/ComboBox.aspx PinsussAnonymous3:44 27 May '05  
GeneralCombobox in a table Pinmemberpatriceemery12:12 6 May '05  
GeneralI love it!!! But ... Pinmemberpatriceemery19:24 1 May '05  
GeneralRe: I love it!!! But ... Pinmemberczeh7:23 17 Jun '05  
GeneralGood work - Shame its not crossbrowser complient PinmemberIan Powell6:52 19 Apr '05  
GeneralRe: Good work - Shame its not crossbrowser complient PinmemberDieterAIV1:17 3 May '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 6 May 2005
Editor: Smitha Vijayan
Copyright 2005 by bestcomy
Everything else Copyright © CodeProject, 1999-2010
Web17 | Advertise on the Code Project