ASP.NET服务器控件开发简介:ComboBox2011-03-19我在Web项目的开发过程中很多时候都要用到ComboBox,找了很多类似的控件来用发现都不尽如人意,我所希望的能够在WEB上使用的ComboBox应该就是在DropDownList的功能上加入了文本输入功能,我个人比较看重的一点就是下拉列表应该可以伸展到浏览器之外,然而目前大多数的ComboBox要么是用DIV来显示选择项,要么就是用TextBox+ListBox,DIV的方式会不能伸展到浏览器之外,而TextBox+ListBox方式ListBox占用页面空间。后来发现了A DHTML combo box , 于是决定基于此HTC开发一个ASP.NET服务器控件.

关键类设计ComboBox : 关键是有一个Text属性获取控件值,没有SelectedIndexChanged事件(我觉得该事件对ComboBox来说不是很重要,当然你也可以根据你自己的需要增加对此事件的支持),Items属性当然是必不可少的;ComboItem : 相对于ListItem来说我没有设计Value属性;ComboItemCollection : 这是ComboItem的集合类,实现了ICollection接口,其功能类似ListItemCollection。实现ViewState实现ViewState是我个人觉得最有趣的部分,根据类的层次结构,先看看ComboItem如何实现ViewState,其实很简单,就是实现System.Web.UI.IStateManager接口:
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;
}
}