Click here to Skip to main content
15,896,726 members
Articles / Web Development / HTML
Article

Custom ComboBox server control for ASP.NET

Rate me:
Please Sign up or sign in to vote.
3.82/5 (19 votes)
5 May 2005 237.8K   2.3K   44   37
A custom ComboBox control for ASP.NET.

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:

C#
for(int i=1; i < 10; i++)
{
    ComboItem item = new ComboItem("Item"+i.ToString());
    ComboBox1.Items.Add(item);
}
ASP.NET
<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>
C#
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

C#
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

C#
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

C#
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


Written By
Web Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDROPDOWN IS SHOWING IN SMALL SIZE Pin
Sajeeva Jami26-Jun-12 19:57
Sajeeva Jami26-Jun-12 19:57 
GeneralLicense Pin
Helen Siver25-May-09 4:27
Helen Siver25-May-09 4:27 
GeneralCombobox not rendering items properly Pin
wdycee226-Aug-08 16:36
wdycee226-Aug-08 16:36 
I'm trying to use the combobox and load it with a select result from an access table. But, the control displays like a label with all the items displayed at the same time. In design view, it looks like a normal combobox, but as soon as i run the application, it looks like a label. If there are no items in the control, it just becomes transparent and does not show up on running at all.
I ran the application in debug mode and see that the ItemsCollection has all the items in the array, it is only the way it is being displayed that is a problem.
I am trying to understand this problem since a very long time, but haven't been able to. Can anybody help, please?
GeneralRe: Combobox not rendering items properly Pin
bestcomy26-Aug-08 18:22
bestcomy26-Aug-08 18:22 
GeneralRe: Combobox not rendering items properly Pin
wdycee227-Aug-08 15:52
wdycee227-Aug-08 15:52 
GeneralCombo box not loading Pin
ceetee5-May-08 3:42
ceetee5-May-08 3:42 
GeneralASP.NET 2.0 Pin
moskis2716-May-07 4:22
moskis2716-May-07 4:22 
QuestionHow do you support custom prefixes with the options? Pin
howardjr29-Nov-06 17:35
howardjr29-Nov-06 17:35 
AnswerRe: How do you support custom prefixes with the options? Pin
bestcomy29-Nov-06 19:25
bestcomy29-Nov-06 19:25 
GeneralRe: How do you support custom prefixes with the options? Pin
howardjr29-Nov-06 19:53
howardjr29-Nov-06 19:53 
GeneralRe: How do you support custom prefixes with the options? Pin
howardjr1-Dec-06 12:12
howardjr1-Dec-06 12:12 
Generalgood job ,i like it.. Pin
winart11-Sep-06 20:32
winart11-Sep-06 20:32 
GeneralMuch quicker approach Pin
Martin Auve12-Apr-06 5:30
Martin Auve12-Apr-06 5:30 
GeneralRe: Much quicker approach Pin
Dark-ranger13-Jun-06 0:38
Dark-ranger13-Jun-06 0:38 
GeneralRe: Much quicker approach Pin
Martin Auve12-Jul-06 9:52
Martin Auve12-Jul-06 9:52 
GeneralRe: Much quicker approach Pin
Martin Auve12-Jul-06 9:54
Martin Auve12-Jul-06 9:54 
GeneralSelected Index change Pin
Thoths7-Nov-05 23:34
Thoths7-Nov-05 23:34 
GeneralRe: Selected Index change Pin
KrishnaDwivedi4-Jul-06 19:33
KrishnaDwivedi4-Jul-06 19:33 
Questionhow add row to a combobox Pin
malihehabibi10-Oct-05 0:39
malihehabibi10-Oct-05 0:39 
GeneralComboBox WIDTHS!! Pin
DjIndy235-Aug-05 3:16
DjIndy235-Aug-05 3:16 
Generalhttp://www.metabuilders.com/Tools/ComboBox.aspx Pin
Anonymous27-May-05 2:44
Anonymous27-May-05 2:44 
GeneralCombobox in a table Pin
patriceemery6-May-05 11:12
patriceemery6-May-05 11:12 
GeneralI love it!!! But ... Pin
patriceemery1-May-05 18:24
patriceemery1-May-05 18:24 
GeneralRe: I love it!!! But ... Pin
czeh17-Jun-05 6:23
czeh17-Jun-05 6:23 
GeneralGood work - Shame its not crossbrowser complient Pin
Ian Powell19-Apr-05 5:52
Ian Powell19-Apr-05 5:52 

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

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