ViewState相信大家都会使用,可ViewState到底是什么,又有多少人知道呢
StateBag类这个就不用多说啦吧
在Asp.net 2.0 里,用到StateBag有以下几种
2 WebControl.attrState这个是存放Attribute的
3 Style.statebag是存放样式的
4.CssStyleCollection. StateBag
.......
Page生命周期内SaveAllState时
需要先生成个Piar类,
在 调用this.SavePageStateToPersistenceMedium(pair1);时,将其序列化
注意:Asp.net2.0只实现了HiddenFieldPageStatePersister,用户可以从重写,或者使用ControlAdapter提供其它形式的进理机制
HiddenFieldPageStatePersister.Save时会过pair1进行序列化
序列化时,.net提供了三种方式
1使用密钥
2.使用Mac
3不使用
outputStream.SetLength(outputStream.Position);
byte[] buf = outputStream.GetBuffer();
int length = (int)outputStream.Length;
先序列化
//如果未调用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里放的是什么,我画了一幅图,详细说明了一下
namespace System.Web.UI
{
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Reflection;
using System.Security.Permissions;
using System.Web;
using System.Web.Util;
[AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)]
public sealed class StateBag : IStateManager, IDictionary, ICollection, IEnumerable
{
private IDictionary bag;
private bool ignoreCase;
private bool marked;
public StateBag() : this(false)
{
}
public StateBag(bool ignoreCase)
{
this.marked = false;
this.ignoreCase = ignoreCase;
this.bag = this.CreateBag();
}
public StateItem Add(string key, object value)
{
if (string.IsNullOrEmpty(key))
{
throw ExceptionUtil.ParameterNullOrEmpty("key");
}
StateItem item = this.bag[key] as StateItem;
if (item == null)
{
if ((value != null) || this.marked)
{
item = new StateItem(value);
this.bag.Add(key, item);
}
}
else if ((value == null) && !this.marked)
{
this.bag.Remove(key);
}
else
{
item.Value = value;
}
if ((item != null) && this.marked)
{
item.IsDirty = true;
}
return item;
}
public void Clear()
{
this.bag.Clear();
}
private IDictionary CreateBag()
{
return new HybridDictionary(this.ignoreCase);
}
public IDictionaryEnumerator GetEnumerator()
{
return this.bag.GetEnumerator();
}
public bool IsItemDirty(string key)
{
StateItem item = this.bag[key] as StateItem;
return ((item != null) && item.IsDirty);
}
internal void LoadViewState(object state)
{
if (state != null)
{
ArrayList list = (ArrayList) state;
for (int i = 0; i < list.Count; i += 2)
{
string key = ((IndexedString) list[i]).Value;
object obj2 = list[i + 1];
this.Add(key, obj2);
}
}
}
public void Remove(string key)
{
this.bag.Remove(key);
}
internal object SaveViewState()
{
ArrayList list = null;
if (this.bag.Count != 0)
{
IDictionaryEnumerator enumerator = this.bag.GetEnumerator();
while (enumerator.MoveNext())
{
StateItem item = (StateItem) enumerator.Value;
if (item.IsDirty)
{
if (list == null)
{
list = new ArrayList();
}
list.Add(new IndexedString((string) enumerator.Key));
list.Add(item.Value);
}
}
}
return list;
}
public void SetDirty(bool dirty)
{
if (this.bag.Count != 0)
{
foreach (StateItem item in this.bag.Values)
{
item.IsDirty = dirty;
}
}
}
public void SetItemDirty(string key, bool dirty)
{
StateItem item = this.bag[key] as StateItem;
if (item != null)
{
item.IsDirty = dirty;
}
}
void ICollection.CopyTo(Array array, int index)
{
this.Values.CopyTo(array, index);
}
void IDictionary.Add(object key, object value)
{
this.Add((string) key, value);
}
bool IDictionary.Contains(object key)
{
return this.bag.Contains((string) key);
}
void IDictionary.Remove(object key)
{
this.Remove((string) key);
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
void IStateManager.LoadViewState(object state)
{
this.LoadViewState(state);
}
object IStateManager.SaveViewState()
{
return this.SaveViewState();
}
void IStateManager.TrackViewState()
{
this.TrackViewState();
}
internal void TrackViewState()
{
this.marked = true;
}
public int Count
{
get
{
return this.bag.Count;
}
}
internal bool IsTrackingViewState
{
get
{
return this.marked;
}
}
public object this[string key]
{
get
{
if (string.IsNullOrEmpty(key))
{
throw ExceptionUtil.ParameterNullOrEmpty("key");
}
StateItem item = this.bag[key] as StateItem;
if (item != null)
{
return item.Value;
}
return null;
}
set
{
this.Add(key, value);
}
}
public ICollection Keys
{
get
{
return this.bag.Keys;
}
}
bool ICollection.IsSynchronized
{
get
{
return false;
}
}
object ICollection.SyncRoot
{
get
{
return this;
}
}
bool IDictionary.IsFixedSize
{
get
{
return false;
}
}
bool IDictionary.IsReadOnly
{
get
{
return false;
}
}
object IDictionary.this[object key]
{
get
{
return this[(string) key];
}
set
{
this[(string) key] = value;
}
}
bool IStateManager.IsTrackingViewState
{
get
{
return this.IsTrackingViewState;
}
}
public ICollection Values
{
get
{
return this.bag.Values;
}
}
}
}