ASP.NET控件開發(fā)基礎(chǔ)之為子控件添加樣式
上一篇討論了視圖狀態(tài)的用法,讓我們再回到第八篇的時候.從第八篇的時候跳了很大篇幅來繼續(xù)講屬性,然后接著講類型轉(zhuǎn)換器,再接著講視圖狀態(tài).繞到現(xiàn)在才接著講復(fù)合控件的樣式的使用,因為上面講的東西是緊密聯(lián)系的.如果已經(jīng)理解自定義視圖狀態(tài)管理,那這一篇則看起來相關(guān)的簡單.
ASP.NET控件開發(fā)基礎(chǔ)之為子控件添加樣式1.復(fù)合控件中樣式屬性概述
在第六篇的時候已經(jīng)介紹過樣式的使用了,在復(fù)合控件中你同樣可以用此方法給控件定義多個樣式屬性,但此方法很適合像label這樣非復(fù)合控件.
當(dāng)然復(fù)合控件可以適當(dāng)?shù)亩x其自身的樣式屬性,同時你還需要為其子控件提供樣式,典型的控件如GridView控件,如下圖
它有很多不同種類的列,而每種不同的列則有不同的樣式集合屬性,如果將其每個樣式屬性均暴露為***屬性,那樣式屬性將變得很混亂.
我們可以用此方法為復(fù)合控件的子控件定義樣式,實現(xiàn)每個子控件對應(yīng)Style類型的復(fù)雜樣式屬性,將樣式屬性暴露為復(fù)合控件的***屬性,這樣更容易管理復(fù)合控件樣式屬性.
ASP.NET控件開發(fā)基礎(chǔ)之為子控件添加樣式2.復(fù)合控件中樣式屬性實現(xiàn)(為子控件提供樣式)
Style類本身繼承IStateManager 接口,并實現(xiàn)了接口方法.在第五篇我們曾重寫CreateControlStyle方法,如下
- protected override Style CreateControlStyle()
- {
- return new Style(ViewState);
- }
其初始化的時候即存儲樣式信息在視圖狀態(tài)中,而其自定義的樣式的狀態(tài)管理機制則跟上一篇非常的相似.你需要重寫Control類的狀態(tài)管理的幾個方法來實現(xiàn)樣式的狀態(tài)管理.還是以登錄控件為例.
(1)先自定義樣式集合屬性
定義方法跟上一篇視圖狀態(tài)中的Address屬性很相似
如下代碼
- #region 樣式屬性
- [
- Category("Styles"),
- DefaultValue(null),
- DesignerSerializationVisibility(
- DesignerSerializationVisibility.Content),
- PersistenceMode(PersistenceMode.InnerProperty),
- Description(
- "應(yīng)用于按鈕的樣式")
- ]
- 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(
- "應(yīng)用于文本框的樣式")
- ]
- public virtual Style TextBoxStyle
- {
- get
- {
- if (_textBoxStyle == null)
- {
- _textBoxStyle = new Style();
- if (IsTrackingViewState)
- {
- ((IStateManager)_textBoxStyle).TrackViewState();
- }
- }
- return _textBoxStyle;
- }
- }
- #endregion
(2)自定義視圖狀態(tài)管理
因為此處定義了兩個樣式集合屬性,所以用到了Triplet這個輔助類,其跟Pair類一樣都是輔助類,而其可以存儲三個相關(guān)對象的基本結(jié)構(gòu).如果你要儲存三個以上就不能用這兩個輔助類了,實現(xiàn)方法還是很簡單的.
如下代碼
- #region 自定義視圖狀態(tài)
- 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
(3)為子控件添加樣式集合屬性
上面工作做好后,然后你就可以在呈現(xiàn)方法Render方法或RenderContent方法中為子控件添加樣式集合屬性,如下代碼
- if (_buttonStyle != null)
- {
- submitButton.ApplyStyle(ButtonStyle);
- }
- if (_textBoxStyle != null)
- {
- nameTextBox.ApplyStyle(TextBoxStyle);
- emailTextBox.ApplyStyle(TextBoxStyle);
- }
來看一下效果,屬性面板已經(jīng)有子控件樣式集合屬性了,這樣就更容易管理樣式了.
定義子控件樣式就這么的簡單,主要難點還是在于自定義視圖狀態(tài)管理,對自定義視圖狀態(tài)管理熟悉的話,看到這里肯定很簡單,如果沒看明白就須先弄懂如何自定義視圖狀態(tài)管理.
注意點:ASP.NET中復(fù)合控件可以直接繼承CompositeControl類即可,大家可以了解一下此類。
ASP.NET控件開發(fā)基礎(chǔ)之為子控件添加樣式的基本情況就向你介紹到這里,希望你對ASP.NET控件開發(fā)基礎(chǔ)之為子控件添加樣式有所了解。
【編輯推薦】