我们首先要澄清的第一个概念是页面类Page本质是一个控件类,它派生于TemplateControl类,而TemplateControl派生自Control类。既然饭我没有乱吃,自然话也不会乱讲。借田有良老师的翠花给大家上证据如下:
从设计模式的角度讲,页面模型是一个“合成模式(Composite)”,它本身是一棵由多层控件组成的结构树,顶层是Page,以下有叶有树枝,叶是不再包涵子控件的控件,枝是又包涵子控件的控件,每一层控件的"初始化\加载视图状态\加载\预呈现\保存视图状态\卸载"等方法都会调用子控件的对应方法,父控件调用子控件的方法,子又调用孙的,如此递归。
1、初始化:
internal virtual void InitRecursive(Control namingContainer)
{ this.ResolveAdapter();if (this._controls != null)
{if (this.flags[128])
{ namingContainer = this;}
string collectionReadOnly = this._controls.SetCollectionReadOnly("Parent_collections_readonly");
int count = this._controls.Count;
for (int i = 0; i < count; i++)
{ Control control = this._controls[i];control.UpdateNamingContainer(namingContainer);
if (control._id == null && namingContainer != null && !control.flags[64])
{control.GenerateAutomaticID();
}
control._page = this.Page;control.InitRecursive(namingContainer);
}
this._controls.SetCollectionReadOnly(collectionReadOnly);}
if (this._controlState < ControlState.Initialized)
{ this._controlState = ControlState.ChildrenInitialized;if (this.Page != null && !this.DesignMode && this.Page.ContainsTheme && this.EnableTheming)
{this.ApplySkin(this.Page);
}
if (this.AdapterInternal != null)
{ this.AdapterInternal.OnInit(EventArgs.Empty);}
else { this.OnInit(EventArgs.Empty);}
this._controlState = ControlState.Initialized;}
this.TrackViewState();}
2、加载视图状态:
internal void LoadViewStateRecursive(object savedState)
{if (savedState == null || this.flags[4])
{ return;}
if (this.Page != null && this.Page.IsPostBack)
{object obj = null;
Pair pair = savedState as Pair; object first;ArrayList arrayList;
if (pair != null)
{first = pair.First;
arrayList = (ArrayList)pair.Second;
}
else {Triplet triplet = (Triplet)savedState;
first = triplet.First;
obj = triplet.Second;
arrayList = (ArrayList)triplet.Third;
}
try {if (obj != null && this.AdapterInternal != null)
{ this.AdapterInternal.LoadAdapterViewState(obj);}
if (first != null)
{ this.LoadViewState(first);}
if (arrayList != null)
{if (this.LoadViewStateByID)
{ this.LoadChildViewStateByID(arrayList);}
else { this.LoadChildViewStateByIndex(arrayList);}
}
}
catch (InvalidCastException) {throw new HttpException(SR.GetString("Controls_Cant_Change_Between_Posts"));
}
catch (IndexOutOfRangeException) {throw new HttpException(SR.GetString("Controls_Cant_Change_Between_Posts"));
}
}
this._controlState = ControlState.ViewStateLoaded;}
internal void LoadChildViewStateByID(ArrayList childState)
{ int count = childState.Count;for (int i = 0; i < count; i += 2)
{string text = (string)childState[i];
object obj = childState[i + 1]; Control control = this.FindControl(text);if (control != null)
{control.LoadViewStateRecursive(obj);
}
else { this.EnsureOccasionalFields();if (this._occasionalFields.ControlsViewState == null)
{this._occasionalFields.ControlsViewState = new Hashtable();
}
this._occasionalFields.ControlsViewState[text] = obj;}
}
}
3、加载:
internal virtual void LoadRecursive()
{if (this._controlState < ControlState.Loaded)
{if (this.AdapterInternal != null)
{ this.AdapterInternal.OnLoad(EventArgs.Empty);}
else { this.OnLoad(EventArgs.Empty);}
}
if (this._controls != null)
{string collectionReadOnly = this._controls.SetCollectionReadOnly("Parent_collections_readonly");
int count = this._controls.Count;
for (int i = 0; i < count; i++)
{ this._controls[i].LoadRecursive();}
this._controls.SetCollectionReadOnly(collectionReadOnly);}
if (this._controlState < ControlState.Loaded)
{ this._controlState = ControlState.Loaded;}
}
4、预呈现:
internal virtual void PreRenderRecursiveInternal()
{if (!this.Visible)
{ this.flags.Set(16);}
else { this.flags.Clear(16); this.EnsureChildControls();if (this.AdapterInternal != null)
{ this.AdapterInternal.OnPreRender(EventArgs.Empty);}
else { this.OnPreRender(EventArgs.Empty);}
if (this._controls != null)
{string collectionReadOnly = this._controls.SetCollectionReadOnly("Parent_collections_readonly");
int count = this._controls.Count;
for (int i = 0; i < count; i++)
{ this._controls[i].PreRenderRecursiveInternal();}
this._controls.SetCollectionReadOnly(collectionReadOnly);}
}
this._controlState = ControlState.PreRendered;}
internal object SaveViewStateRecursive(ViewStateMode inheritedMode)
{if (this.flags[4])
{return null;
}
bool flag;if (this.flags[8388608])
{if (this.flags[16777216])
{ flag = true;inheritedMode = ViewStateMode.Enabled;
}
else { flag = false;inheritedMode = ViewStateMode.Disabled;
}
}
else {flag = (inheritedMode == ViewStateMode.Enabled);
}
object obj = null;
object obj2 = null;
if (flag) {if (this.AdapterInternal != null)
{ obj = this.AdapterInternal.SaveAdapterViewState();}
obj2 = this.SaveViewState();}
ArrayList arrayList = null;if (this.HasControls())
{ ControlCollection controls = this._controls; int count = controls.Count;bool loadViewStateByID = this.LoadViewStateByID;
for (int i = 0; i < count; i++)
{Control control = controls[i];
object obj3 = control.SaveViewStateRecursive(inheritedMode);if (obj3 != null)
{if (arrayList == null)
{ arrayList = new ArrayList(count);}
if (loadViewStateByID) {control.EnsureID();
arrayList.Add(control.ID);
}
else {arrayList.Add(i);
}
arrayList.Add(obj3);
}
}
}
if (this.AdapterInternal != null)
{if (obj2 != null || obj != null || arrayList != null)
{return new Triplet(obj2, obj, arrayList);
}
}
else {if (obj2 != null || arrayList != null)
{return new Pair(obj2, arrayList);
}
}
return null;
}
6、卸载:
internal virtual void UnloadRecursive(bool dispose)
{ Page page = this.Page;if (page != null && page.RequiresControlState(this))
{ page.UnregisterRequiresControlState(this);this.RareFieldsEnsured.RequiredControlState = true;
}
if (this.flags[2097152])
{this._id = null;
this.flags.Clear(2097152);}
if (this._controls != null)
{string collectionReadOnly = this._controls.SetCollectionReadOnly("Parent_collections_readonly");
int count = this._controls.Count;
for (int i = 0; i < count; i++)
{ this._controls[i].UnloadRecursive(dispose);}
this._controls.SetCollectionReadOnly(collectionReadOnly);}
if (this.AdapterInternal != null)
{ this.AdapterInternal.OnUnload(EventArgs.Empty);}
else { this.OnUnload(EventArgs.Empty);}
if (dispose) { this.Dispose();}
if (this.IsReloadable)
{ this._controlState = ControlState.Constructed;}
}
第一篇:初识ASP.NET控件开发_第三节:“生死有序”的控件生命周期
原文:http://www.cnblogs.com/hanzhaoxin/p/4046951.html