asp.net控件開發基礎(12)
從第八篇的時候跳了很大篇幅來繼續講屬性,然后接著講類型轉換器,再接著講視圖狀態.繞到現在才接著講復合控件的樣式的使用,因為上面講的東西是緊密聯系的.如果已經理解自定義視圖狀態管理,那這一篇則看起來相關的簡單.
1.復合控件中樣式屬性概述
在第六篇的時候已經介紹過樣式的使用了,在復合控件中你同樣可以用此方法給控件定義多個樣式屬性,但此方法很適合像label這樣非復合控件。當然復合控件可以適當的定義其自身的樣式屬性,同時你還需要為其子控件提供樣式,典型的控件如GridView控件,如下圖
它有很多不同種類的列,而每種不同的列則有不同的樣式集合屬性,如果將其每個樣式屬性均暴露為頂級屬性,那樣式屬性將變得很混亂。我們可以用此方法為復合控件的子控件定義樣式,實現每個子控件對應Style類型的復雜樣式屬性,將樣式屬性暴露為復合控件的頂級屬性,這樣更容易管理復合控件樣式屬性。
2.復合控件中樣式屬性實現(為子控件提供樣式)
Style類本身繼承IStateManager 接口,并實現了接口方法.在第五篇我們曾重寫CreateControlStyle方法,如下





(1)先自定義樣式集合屬性
定義方法跟上一篇視圖狀態中的Address屬性很相似
如下代碼
[
Category("Styles"),
DefaultValue(null),
DesignerSerializationVisibility(
DesignerSerializationVisibility.Content),
PersistenceMode(PersistenceMode.InnerProperty),
Description(
"應用于按鈕的樣式")
]
public virtual Style ButtonStyle
{
get
{
if (_buttonStyle == null)
{
_buttonStyle = new Style();
if (IsTrackingViewState)
{
((IStateManager)_buttonStyle).TrackViewState();
}
}
return _buttonStyle;
}
}
[
Category("Styles"),
DefaultValue(null),
DesignerSerializationVisibility(
DesignerSerializationVisibility.Content),
PersistenceMode(PersistenceMode.InnerProperty),
Description(
"應用于文本框的樣式")
]
public virtual Style TextBoxStyle
{
get
{
if (_textBoxStyle == null)
{
_textBoxStyle = new Style();
if (IsTrackingViewState)
{
((IStateManager)_textBoxStyle).TrackViewState();
}
}
return _textBoxStyle;
}
}
#endregion
(2)自定義視圖狀態管理
因為此處定義了兩個樣式集合屬性,所以用到了Triplet這個輔助類,其跟Pair類一樣都是輔助類,而其可以存儲三個相關對象的基本結構.如果你要儲存三個以上就不能用這兩個輔助類了,實現方法還是很簡單的.
如下代碼
protected override void LoadViewState(object savedState)
{
if (savedState == null)
{
base.LoadViewState(null);
return;
}
else
{
Triplet t = savedState as Triplet;
if (t != null)
{
base.LoadViewState(t.First);
if ((t.Second) != null)
{
((IStateManager)ButtonStyle).LoadViewState(t.Second);
}
if ((t.Third) != null)
{
((IStateManager)TextBoxStyle).LoadViewState(t.Third);
}
}
else
{
throw new ArgumentException("Invalid view state .");
}
}
}
protected override object SaveViewState()
{
object baseState = base.SaveViewState();
object buttonStyleState = null;
object textBoxStyleState = null;
if (_buttonStyle != null)
{
buttonStyleState =
((IStateManager)_buttonStyle).SaveViewState();
}
if (_textBoxStyle != null)
{
textBoxStyleState =
((IStateManager)_textBoxStyle).SaveViewState();
}
return new Triplet(baseState,
buttonStyleState, textBoxStyleState);
}
protected override void TrackViewState()
{
base.TrackViewState();
if (_buttonStyle != null)
{
((IStateManager)_buttonStyle).TrackViewState();
}
if (_textBoxStyle != null)
{
((IStateManager)_textBoxStyle).TrackViewState();
}
}
#endregion
上面工作做好后,然后你就可以在呈現方法Render方法或RenderContent方法中為子控件添加樣式集合屬性,如下代碼











定義子控件樣式就這么的簡單,主要難點還是在于自定義視圖狀態管理,對自定義視圖狀態管理熟悉的話,看到這里肯定很簡單,如果沒看明白就須先弄懂如何自定義視圖狀態管理。
注意點:asp.net2.0中復合控件可以直接繼承CompositeControl類即可,大家可以了解一下此類
本文的示例代碼來自MSDN2005中的代碼。
下一篇:asp.net控件開發基礎(13)