ComboBox [open source code]

Source: Internet
Author: User
Introduction

In the development process of WEB projects, I often use ComboBox. I found many similar controls and found that they were not satisfactory, the ComboBox that I want to use on the web should be the text input function added to the dropdownlist function. One of my personal values is that the drop-down list should be extended beyond the browser, however, most of the current comboboxes either use Div to display the selected items, or use textbox + ListBox. The DIV method cannot be extended beyond the browser, the textbox + ListBox method occupies the page space. Later I found a DHTML combo box, so I decided to develop an ASP. NET Server Control Based on HTC.

Key Design

  • ComboBox: the key is that there is a text attribute to get the control value without the selectedindexchanged event (I think this event is not very important to ComboBox, of course, you can also add support for this event based on your own needs), the items attribute is of course essential;
  • Comboitem: Compared to listitem, I have not designed the value attribute;
  • Comboitemcollection: This is the collection class of comboitem. It implements the icollection interface, and its function is similar to listitemcollection.

Implement viewstate

Implementing viewstate is the most interesting part I personally think. Based on the hierarchy of classes, Let's first look at how comboitem implements viewstate. In fact, it is very easy to implement the system. Web. UI. istatemanager interface:

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;
}
}

The saveviewstate () method saves the text and selected attributes of comboitem, while loadviewstate (object state) restores these two attributes.

Next let's take a look at the comboitemcollection, which also implements the istatemanager interface:

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);
}
}
}
}

The saveviewstate () method saves the view State one by one, and the loadviewstate (object state) method restores the view State one by one.

Finally, let's take a look at ComboBox and rewrite the following methods:

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;
}
}

The saveviewstate () method saves the view State of the base class, saves the items and text attributes to the view state, and restores the loadviewstate (Object savedstate) method.

Implement ipostbackdatahandler Interface

public void RaisePostDataChangedEvent()
{
}

public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
string[] textArray1 = postCollection.GetValues(postDataKey);
if (textArray1 != null)
{
this.ClearSelection();
ComboItem item = this.FindByText(textArray1[0]);
if (item != null)
{
item.Selected = true;
}
_text = textArray1[0];
}
return false;
}

Implement Data Binding

I am very concerned about how to bind data like dropdownlist. The key is to override the ondatabinding method and add a class performancehelper to parse the data source.

protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);
IEnumerable enumerable1 = DataSourceHelper.GetResolvedDataSource(this.DataSource, this.DataMember);
if (enumerable1 != null)
{
this.Items.Clear();
foreach (object obj1 in enumerable1)
{
ComboItem item = new ComboItem();
if (this.DataTextField != string.Empty)
item.Text = DataBinder.GetPropertyValue(obj1, this.DataTextField, null);
else
item.Text = obj1.ToString();
this.Items.Add(item);
}
}
}

Three methods
Programming Method:

for (int i = 1; i < 10; i++)
{
ComboItem item = new ComboItem("Item" + i.ToString());
ComboBox1.Items.Add(item);
}

Data Binding method

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();

Direct page declaration

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

Source code download
Http://www.cnblogs.com/Files/bestcomy/WebControlTest.rar
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.