zoukankan      html  css  js  c++  java
  • 一起谈.NET技术,asp.net控件开发基础(12) 狼人:

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

      1.复合控件中样式属性概述

      在第六篇的时候已经介绍过样式的使用了,在复合控件中你同样可以用此方法给控件定义多个样式属性,但此方法很适合像label这样非复合控件。当然复合控件可以适当的定义其自身的样式属性,同时你还需要为其子控件提供样式,典型的控件如GridView控件,如下图

      它有很多不同种类的列,而每种不同的列则有不同的样式集合属性,如果将其每个样式属性均暴露为顶级属性,那样式属性将变得很混乱。我们可以用此方法为复合控件的子控件定义样式,实现每个子控件对应Style类型的复杂样式属性,将样式属性暴露为复合控件的顶级属性,这样更容易管理复合控件样式属性。

      2.复合控件中样式属性实现(为子控件提供样式)

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

            protected override Style CreateControlStyle()
            
    {

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

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

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

      如下代码

    #region 样式属性
    [
    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类一样都是辅助类,而其可以存储三个相关对象的基本结构.如果你要储存三个以上就不能用这两个辅助类了,实现方法还是很简单的.

    如下代码

    #region 自定义视图状态
    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)为子控件添加样式集合属性

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

    if (_buttonStyle != null)
                
    {
                    submitButton.ApplyStyle(ButtonStyle);
                }


                
    if (_textBoxStyle != null)
                
    {
                    nameTextBox.ApplyStyle(TextBoxStyle);
                    emailTextBox.ApplyStyle(TextBoxStyle);
                }
    来看一下效果,属性面板已经有子控件样式集合属性了,这样就更容易管理样式了.

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

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

      本文的示例代码来自MSDN2005中的代码。

    上一篇:asp.net控件开发基础(11)

    下一篇:asp.net控件开发基础(13)
  • 相关阅读:
    45
    布里斯班初体验
    走出你的舒适区
    Homebrew- MAC上的包管理利器
    Java经典类库-Guava中的函数式编程讲解
    使用WebDriver遇到的那些坑
    CheckStyle, 强制你遵循编码规范
    利用php的register_shutdown_function来记录php的输出日志
    PHP-redis中文文档
    php中set_time_limit()函数运用
  • 原文地址:https://www.cnblogs.com/waw/p/2162818.html
Copyright © 2011-2022 走看看