viewstate
{
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;
}
从上面代码看只有 IsDirty=true(发生改变的数据)才会被序列化保存在隐藏字段中
{
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;
}
{
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);
}
}
下面我们再看一下,ViewState是如何恢复值的
{
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);
}
}
}
list.Add(new IndexedString((string) enumerator.Key));
list.Add(item.Value);
所以恢复到ViewState的时候必须间隔一下取值.
应用:
1 this.PerformPreInit();
this.OnPreInit(EventArgs.Empty);
this.InitializeThemes();
this.ApplyMasterPage();
this._preInitWorkComplete = true;
2 this.InitRecursive(null);
control.GenerateAutomaticID();
control._page = this.Page;
control.InitRecursive(namingContainer);
this.OnInit(EventArgs.Empty);
this.TrackViewState();
3 if (this.IsPostBack)
this.LoadAllState();------>base.LoadViewStateRecursive(second.Second);
this.ProcessPostData(this._requestValueCollection, true);------->
4LoadRecursive()
this.OnLoad(EventArgs.Empty);
this._occasionalFields.Controls[i].LoadRecursive();
5if (this.IsPostBack)
this.ProcessPostData(this._leftoverPostData, false);
this.RaiseChangedEvents();
this.RaisePostBackEvent(this._requestValueCollection);
6this.PreRenderRecursiveInternal();
this.OnPreRender(EventArgs.Empty);
this._occasionalFields.Controls[i].PreRenderRecursiveInternal();