C++ C++ C# C# ASP.NET Security ASP.NET Security ASM ASM Скачать Скачать Поиск Поиск Хостинг Хостинг  
  Программа для работы с LPT портом...
Язык: .NET — ©Alexey...
  "ASP.NET Atlas" – AJAX в исполнении Micro...
Язык: .NET — ©legigor@mail.ru...
  "Невытесняющая" Многопоточность...
Язык: C/C++ — ©...
  Update World C++: Сборник GPL QT исходников
  Весь сайт целиком можно загрузить по ссылкам из раздела Скачать
Дебетовая карта Home Credit [CPS] RU

 Custom ComboBox server control for ASP.NET / Custom Controls / 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:

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.



Дебетовая карта Home Credit [CPS] RU