WebControl.SaveViewState是如何保存视图信息的

我们在Control.SaveViewState是如何保存视图信息的中说的是Control的SaveViewState方法,WebControl继承自Control,WebControl重写了Control.SaveViewState方法,很多web控件都继承于WebControl,所以我们有必要说一下WebControl.SaveViewState()方法,这个方法的源码如下:

private  StateBag attrState; 
 1          /**/ /**/ /**/ /// <internalonly/> 
 2        /// <devdoc> 
 3        ///    <para>A protected method. Saves any
 4        ///       state that was modified after the TrackViewState method was invoked.</para> 
 5        /// </devdoc>

 6          protected   override   object  SaveViewState()  {
 7            Pair myState = null;
 8 
 9            // Save values cached out of view state
10            if (_webControlFlags[disabledDirty]) 
11                ViewState["Enabled"= !flags[isWebControlDisabled]; 
12            }

13 
14            if (ControlStyleCreated) {
15                // the style shares the StateBag of its owner WebControl
16                // call SaveViewState to let style participate in state management
17                ControlStyle.SaveViewState(); 
18            }

19 
20            object baseState = base.SaveViewState(); 
21            object aState = null;
22            if (attrState != null
23                aState = attrState.SaveViewState();
24            }

25
26            if (baseState != null || aState != null
27                myState = new Pair(baseState, aState);
28            }
 
29            return myState; 
30        }

代码的第10行,判断控件的Enabled属性。
代码的第14行,通过ControlStyleCreated的值指示是否已为 ControlStyle 属性创建了 Style 对象。
代码的第20行,表示调用Control.SaveViewState()方法。
我们需要注意的地方地方是ControlStyle和attrState里边存储的都是什么数据?我们平时的哪些操作和这两个集合有关系?
我们先说说ControlStyle,我们通过这个属性存储Web 服务器控件的样式。
WebControl.ControlStyle的源码:

 1          /**/ /**/ /**/ /// <devdoc> 
 2        ///    <para>Gets the style of the Web control.</para>
 3        /// </devdoc> 

 4         [ 
 5         Browsable( false ),
 6         DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), 
 7         WebSysDescription(SR.WebControl_ControlStyle)
 8         ]
 9          public  Style ControlStyle  {
10            get 
11                if (controlStyle == null{
12                    controlStyle = CreateControlStyle(); 
13                    if (IsTrackingViewState) 
14                        controlStyle.TrackViewState();
15                    }
 
16                    if (_webControlFlags[deferStyleLoadViewState]) {
17                        _webControlFlags.Clear(deferStyleLoadViewState);
18                        controlStyle.LoadViewState(null);
19                    }
 
20                }

21                return controlStyle; 
22            }
 
23        }
Control.CreateControlStyle()源码:
1          /**/ /**/ /**/ /// <devdoc>
2        ///    <para> A protected method. Creates the style object that is used internally 
3        ///       to implement all style-related properties. Controls may override to create an 
4        ///       appropriately typed style.</para>
5        /// </devdoc> 

6          protected   virtual  Style CreateControlStyle()  {
7            return new Style(ViewState);
8        }

从以上两个方法可以看出,ControlStyle返回的是Style类型的对象,在实例化Style的过程中,需要ViewState属性,我们知道WebControl的ViewState是继承自Control。我们平时对控件的样式进行操作。例如如下代码:

     void  Page_Load(Object sender, EventArgs e)
    
{
        
this.TextBox1.BackColor = System.Drawing.Color.Red;
    }

就是通过Style对象,将值存入WebControl.ViewState。

我们说过了ControlStyle属性,下边说说attrState字段,attrState是在WebControl声明的StateBag类型的字段,Attributes属性和attrState字段相关联,但并不是直接调用它,而是通过AttributeCollection,我们一会看看源代码就清楚多了。微软对Attributes的解释为:控件的属性不对应的任意特性(只用于呈现)的集合。老实说,这个解释不好理解,所幸的是,这个属性我们平时用的比较多,理解起来就好多了。
Attributes属性的源码为:

 1 Code
 2          /**/ /**/ /**/ /// <devdoc> 
 3        ///    <para>Gets the collection of attribute name/value pairs expressed on a Web control but
 4        ///       not supported by the control's strongly typed properties.</para>
 5        /// </devdoc>

 6         [ 
 7         Browsable( false ),
 8         DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), 
 9         WebSysDescription(SR.WebControl_Attributes) 
10         ]
11          public  AttributeCollection Attributes 
12            get {
13                if (attrColl == null{
14
15                    if (attrState == null
16                        attrState = new StateBag(true);
17                        if (IsTrackingViewState) 
18                            attrState.TrackViewState(); 
19                    }

20 
21                    attrColl = new AttributeCollection(attrState);
22                }

23                return attrColl;
24            }
 
25        }

下边还是用代码说明吧,看看Attributes的用法都体现在哪些地方:

 1      void  Page_Load(Object sender, EventArgs e)
 2      {
 3        //第一种用法
 4        TextBox1.Attributes["onblur"= "javascript:alert('Hello! Focus lost from text box!!');";
 5        //第二种用法
 6        TextBox1.Attributes.Add("onblur""javascript:alert('Hello! Focus lost from text box!!');");
 7        //如果和控件的样式相关,请这样使用
 8        TextBox1.Attributes.CssStyle.Add(HtmlTextWriterStyle.Cursor, "hand;");
 9        //如果您要添加的样式在HtmlTextWriterStyle中不存在,写法类似下面所写
10        TextBox1.Attributes.CssStyle.Add("otherStyle""style");
11    }

WebControl.SaveViewState中主要的东西我们都已经说过了,就不难理解其方法的内涵。

转载于:https://www.cnblogs.com/fengfeng/articles/1146531.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值