当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > Asp.net 2.0 ViewState原理

ASP.NET
析构函数的作用 什么是析构函数
asp.net下Request.QueryString取不到值的解决方法
设置DropDownList的当前选项
生成多字段排序分页的SQL的通用类
注册表中存储数据库链接字符串的方法
asp.net下百度的编码和解码
asp.net 操作excel的实现代码
用ASP.NET做的个性化的邮件发送系统
DotNet2.0 生成网站的测试
ASP.net 验证码实现代码(C#)
C#中string与byte[]的转换帮助类-.NET教程,C#语言
把jQuery的each(callback)方法移植到c#中
asp.net对URL含有中文参数的转换
C#语言初级入门介绍
C#编码好习惯小结
C#声明方法实例说明
C#使用正则表达式实例
C#列出局域网中可用SQL Server服务器(续)
将DataRow转成指定类型的类,并返回这个类的对象(带值)
asp.net OleDbCommand 的用法

ASP.NET 中的 Asp.net 2.0 ViewState原理


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 106 ::
收藏到网摘: n/a

ViewState相信大家都会使用,可ViewState到底是什么,又有多少人知道呢?

StateBag类这个就不用多说啦吧

在Asp.net 2.0 里,用到StateBag有三处

1 Control._viewState  这个就是大家使用的ViewState

2 WebControl.attrState这个是存放Attribute的

3 Style.statebag是存放样式的
.......
Page生命周期内SaveAllState时

需要先生成个Piar类,在调用this.SavePageStateToPersistenceMedium(pair1);时,将其序列化

注意:Asp.net2.0只实现了HiddenFieldPageStatePersister,用户可以从重写,或者使用ControlAdapter提供其它形式的进理机制

HiddenFieldPageStatePersister.Save时会过pair1进行序列化

序列化时,.net提供了三种方式

1使用密钥

2.使用Mac

3不使用

  //先序列化
  this.Serialize(outputStream, stateGraph);

 outputStream.SetLength(outputStream.Position);
 byte[] buf = outputStream.GetBuffer();

  int length = (int)outputStream.Length;
          
//判断当前Page.RequiresViewStateEncryptionInternal属性返回值是不是需要加密            
//如果未调用Page.RegisterRequiresViewStateEncryption,则默认为false               
//如果界面设置了RegisterRequiresViewStateEncryption和EnableViewStateMac,加密优先于Mac
               
if ((this._page != null) && this._page.RequiresViewStateEncryptionInternal)    //加密               
{                   
    buf = MachineKeySection.EncryptOrDecryptData(true, buf, this.GetMacKeyModifier(), 0, length);                   
    length = buf.Length;              
}              
else if (((this._page != null) && this._page.EnableViewStateMac) || (this._macKeyBytes != null))//设置可以使用Mac               
{                   
    buf = MachineKeySection.GetEncodedData(buf, this.GetMacKeyModifier(), 0, ref length);               
}

text = Convert.ToBase64String(buf, 0, length); // null of either

谈到这,很多人要问pair1里放的是什么,我画了一幅图,详细说明了一下