一起谈.NET技术,asp.net控件开发基础(12)

本文详细介绍了ASP.NET复合控件中的样式属性使用,通过自定义样式集合属性和视图状态管理简化样式配置。包括样式属性概述、实现步骤以及展示效果,帮助开发者更高效地管理复合控件的样式。

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

  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)

转载于:https://www.cnblogs.com/waw/archive/2011/09/01/2162818.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值