asp.net控件開發基礎(10)
集合屬性相信大家都很熟悉也很常用,如DropDownList,ListBox等控件





1.實現集合屬性效果
經過前面幾篇的學習,相信這一篇看起來已經相對簡單了.我們要做的就是,先定義一個復雜屬性,然后用迭代語句獲取數組數據即可。如果看過前面幾篇就看看下面代碼吧,相信看起來很簡單,我們模仿一個DropDownList,為其屬性添加背景屬性,代碼如下
先定義一個集合屬性,如下






























































然后自定義一個控件,輸出集合屬性,如下代碼
public class DropColor:WebControl
{
private ArrayList dropItemList;
[
Category("Behavior"),
Description("項集合"),
DesignerSerializationVisibility(
DesignerSerializationVisibility.Content),
PersistenceMode(PersistenceMode.InnerDefaultProperty),
]
//定義集合屬性
public ArrayList DropItemList
{
get
{
if (dropItemList == null)
{
dropItemList = new ArrayList();
}
return dropItemList;
}
}
//重寫標簽
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Select;
}
}
protected override void RenderContents(HtmlTextWriter writer)
{
//輸出集合屬性
foreach (DropItem item in dropItemList)
{
DropItem dr = item as DropItem;
if (dropItemList != null && dropItemList.Count > 0)
{
//顏色轉換
WebColorConverter wcc = new WebColorConverter();
writer.AddAttribute(HtmlTextWriterAttribute.Value, dr.Value.ToString());
writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, wcc.ConvertToString(dr.BackColor));
writer.RenderBeginTag(HtmlTextWriterTag.Option);
writer.Write(dr.Text.ToString());
writer.RenderEndTag();
}
}
base.RenderContents(writer);
}
}
上面代碼注意顏色類型之間的轉換,以下為HTML代碼







輸出以后的效果如下圖
效果還不錯吧,而且挺實用的.
2.定義編輯器
大家一般在添加集合屬性的時候往往會在.net自帶的編輯器中添加數據,這樣可以提高效果,不用在HTML視圖添加數據.如下圖
我們也可以為其添加自定義的編輯器,其實我們一直在用.net自帶的編輯器,我們稱之為 UI編輯器或視圖編輯器,如顏色,時間,超級鏈接等,大家該有所體會。其編輯器的基類為位于System.Drawing.Design命名空間的UITypeEditor,很多UI編輯器都從此類派生,有興趣的可以去了解下這個類。
你們如何使用使用UI編輯器呢?也跟上一篇講的類型轉換器一樣,你先要定義一個UI編輯器,然后與相關屬性關聯起來。因為所定義的是一個集合類,而.net已經為我們提供了一個集合編輯器的CollectionEditor類,其已經為我們做了很多工作了,我們最簡單的只需重寫幾個方法即可.具體其他屬性和方法請參考MSDN,如下代碼


















然后把集合屬性與編輯器關聯起來,添加了一個EditorAttribute,第一個參數為指定的編輯器類型,第二個為基本類型





















然后再來看下效果,這樣就方面很多了.
如果還不想看到編輯器里的CustomComponents的命名空間的話,你可以像上一篇一樣自定義一個類型轉換器,代碼如下:
{
#region 方法
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture,
object value)
{
if (value == null)
{
return new DropItem();
}
if (value is string)
{
string s = (string)value;
if (s.Length == 0)
{
return new DropItem();
}
return "DropItem";
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(
ITypeDescriptorContext context,
CultureInfo culture, object value, Type destinationType)
{
if (value != null)
{
if (!(value is DropItem))
{
throw new ArgumentException(
"Invalid DropItem", "value");
}
}
if (destinationType == typeof(string))
{
if (value == null)
{
return String.Empty;
}
return "DropItem";
}
return base.ConvertTo(context, culture, value,
destinationType);
}
#endregion
}
然后還是照著步驟把屬性與其關聯起來




再來看下效果
好了,這回講的比較簡單又實用,希望對大家有幫助.大家同時也可以參考MSDN里的例子,下面的示例代碼下載我也加上了MSDN的例子。已經寫了10篇了,我們應該有些基礎了,我想大家該可以做出一些簡單實用的控件了。
下一篇:asp.net控件開發基礎(11)