ASP.NET中怎么为子控件添加样式

发布时间:2021-07-15 15:42:31 作者:Leah
来源:亿速云 阅读:122

ASP.NET中怎么为子控件添加样式,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

ASP.NET控件开发基础之为子控件添加样式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.NET中怎么为子控件添加样式

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

关于ASP.NET中怎么为子控件添加样式问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. css为select添加样式的实现方法
  2. UWP中如何设置控件样式

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

asp.net

上一篇:如何利用nodejs搭建https服务器

下一篇:linux怎么编译安装nginx

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》