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

ASP.NET
blog程序新版本V2.0 Beta完成,提供V1.0全部源码下载
“/”应用程序中的服务器错误
asp.net(c#)复数类(复数加减乘除四则运算)
asp.net(c#)不可访问,因为它受保护级别限制
asp.net(c#) 水仙花数
ASP.NET实现用图片进度条显示投票结果
asp.net(c#) MS AJAX的安装
asp.net(c#)有关 Session 操作的几个误区
ASP.net基础知识之常见错误分析
[.net] 操纵自如-页面内的配合与通信
c# static的全部用法收集整理
使CheckBoxList的Attributes属性生效(修改微软的一个bug)
在ASP.NET中使用Session常见问题集锦
[原创]完美解决Could not load file or assembly ''AjaxPro.2'' or one of its dependencies. 拒绝访问。
在Apache环境下成功的运行ASP.NET的注意事项
ClickOnce DIY全自动更新下载升级的自我实现
asp.net jscript 一句话木马
在ASP.Net中实现flv视频转换的代码
将DataTable中的一行复制到另一个DataTable的方法
在DataTable中执行Select("条件")后,返回DataTable的方法

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 103 ::
收藏到网摘: 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里放的是什么,我画了一幅图,详细说明了一下