ASP.NET控件开发基础之为子控件添加样式

2009-11-27     作者:   编辑:闫蕊        点击进入论坛

  上一篇讨论了视图状态的用法,让我们再回到第八篇的时候.从第八篇的时候跳了很大篇幅来继续讲属性,然后接着讲类型转换器,再接着讲视图状态.绕到现在才接着讲复合控件的样式的使用,因为上面讲的东西是紧密联系的.如果已经理解自定义视图状态管理,那这一篇则看起来相关的简单.

  ASP.NET控件开发基础之为子控件添加样式1.复合控件中样式属性概述

  在第六篇的时候已经介绍过样式的使用了,在复合控件中你同样可以用此方法给控件定义多个样式属性,但此方法很适合像label这样非复合控件.

  当然复合控件可以适当的定义其自身的样式属性,同时你还需要为其子控件提供样式,典型的控件如GridView控件,如下图

  它有很多不同种类的列,而每种不同的列则有不同的样式集合属性,如果将其每个样式属性均暴露为顶级属性,那样式属性将变得很混乱.

  我们可以用此方法为复合控件的子控件定义样式,实现每个子控件对应Style类型的复杂样式属性,将样式属性暴露为复合控件的顶级属性,这样更容易管理复合控件样式属性.

  ASP.NET控件开发基础之为子控件添加样式2.复合控件中样式属性实现(为子控件提供样式)

  Style类本身继承IStateManager 接口,并实现了接口方法.在第五篇我们曾重写CreateControlStyle方法,如下

  

  1. protected override Style CreateControlStyle()
  2. {
  3. return new Style(ViewState);
  4. }

  其初始化的时候即存储样式信息在视图状态中,而其自定义的样式的状态管理机制则跟上一篇非常的相似.你需要重写Control类的状态管理的几个方法来实现样式的状态管理.还是以登录控件为例.

  (1)先自定义样式集合属性

  定义方法跟上一篇视图状态中的Address属性很相似

  如下代码

  

  1. #region 样式属性
  2. [
  3. Category("Styles"),
  4. DefaultValue(null),
  5. DesignerSerializationVisibility(
  6. DesignerSerializationVisibility.Content),
  7. PersistenceMode(PersistenceMode.InnerProperty),
  8. Description(
  9. "应用于按钮的样式")
  10. ]
  11. public virtual Style ButtonStyle
  12. {
  13. get
  14. {
  15. if (_buttonStyle == null)
  16. {
  17. _buttonStyle = new Style();
  18. if (IsTrackingViewState)
  19. {
  20. ((IStateManager)_buttonStyle).TrackViewState();
  21. }
  22. }
  23. return _buttonStyle;
  24. }
  25. }
  26. [
  27. Category("Styles"),
  28. DefaultValue(null),
  29. DesignerSerializationVisibility(
  30. DesignerSerializationVisibility.Content),
  31. PersistenceMode(PersistenceMode.InnerProperty),
  32. Description(
  33. "应用于文本框的样式")
  34. ]
  35. public virtual Style TextBoxStyle
  36. {
  37. get
  38. {
  39. if (_textBoxStyle == null)
  40. {
  41. _textBoxStyle = new Style();
  42. if (IsTrackingViewState)
  43. {
  44. ((IStateManager)_textBoxStyle).TrackViewState();
  45. }
  46. }
  47. return _textBoxStyle;
  48. }
  49. }
  50. #endregion

  (2)自定义视图状态管理

  因为此处定义了两个样式集合属性,所以用到了Triplet这个辅助类,其跟Pair类一样都是辅助类,而其可以存储三个相关对象的基本结构.如果你要储存三个以上就不能用这两个辅助类了,实现方法还是很简单的.

  如下代码

 

  1. #region 自定义视图状态
  2. protected override void LoadViewState(object savedState)
  3. {
  4. if (savedState == null)
  5. {
  6. base.LoadViewState(null);
  7. return;
  8. }
  9. else
  10. {
  11. Triplet t = savedState as Triplet;
  12. if (t != null)
  13. {
  14. base.LoadViewState(t.First);
  15. if ((t.Second) != null)
  16. {
  17. ((IStateManager)ButtonStyle).LoadViewState(t.Second);
  18. }
  19. if ((t.Third) != null)
  20. {
  21. ((IStateManager)TextBoxStyle).LoadViewState(t.Third);
  22. }
  23. }
  24. else
  25. {
  26. throw new ArgumentException("Invalid view state .");
  27. }
  28. }
  29. }
  30. protected override object SaveViewState()
  31. {
  32. object baseState = base.SaveViewState();
  33. object buttonStyleState = null;
  34. object textBoxStyleState = null;
  35. if (_buttonStyle != null)
  36. {
  37. buttonStyleState =
  38. ((IStateManager)_buttonStyle).SaveViewState();
  39. }
  40. if (_textBoxStyle != null)
  41. {
  42. textBoxStyleState =
  43. ((IStateManager)_textBoxStyle).SaveViewState();
  44. }
  45. return new Triplet(baseState,
  46. buttonStyleState, textBoxStyleState);
  47. }
  48. protected override void TrackViewState()
  49. {
  50. base.TrackViewState();
  51. if (_buttonStyle != null)
  52. {
  53. ((IStateManager)_buttonStyle).TrackViewState();
  54. }
  55. if (_textBoxStyle != null)
  56. {
  57. ((IStateManager)_textBoxStyle).TrackViewState();
  58. }
  59. }
  60. #endregion

  (3)为子控件添加样式集合属性

  上面工作做好后,然后你就可以在呈现方法Render方法或RenderContent方法中为子控件添加样式集合属性,如下代码

 

  1. if (_buttonStyle != null)
  2. {
  3. submitButton.ApplyStyle(ButtonStyle);
  4. }
  5. if (_textBoxStyle != null)
  6. {
  7. nameTextBox.ApplyStyle(TextBoxStyle);
  8. emailTextBox.ApplyStyle(TextBoxStyle);
  9. }

  来看一下效果,属性面板已经有子控件样式集合属性了,这样就更容易管理样式了.

  定义子控件样式就这么的简单,主要难点还是在于自定义视图状态管理,对自定义视图状态管理熟悉的话,看到这里肯定很简单,如果没看明白就须先弄懂如何自定义视图状态管理.

  注意点:ASP.NET中复合控件可以直接继承CompositeControl类即可,大家可以了解一下此类。

  ASP.NET控件开发基础之为子控件添加样式的基本情况就向你介绍到这里,希望你对ASP.NET控件开发基础之为子控件添加样式有所了解。

寻找产品:
姓       名: 电   话:
公       司: E-mail:
描       述: