(1)自定義服務器控件繼承下面3個類之一
Control
WebControl
CompositeControl
1.1 Control呈現過程
語法
[BindableAttribute(true)]
[ThemeableAttribute(false)]
public class Control : IComponent, IDisposable, IParserAccessor,
IUrlResolutionService, IDataBindingsAccessor, IControlBuilderAccessor, IControlDesignerAccessor, IExpressionsAccessor
說明
protected internal virtual void Render(HtmlTextWriter writer)
{
this.RenderChildren(writer);
}
protected internal virtual void RenderChildren(HtmlTextWriter writer)
{
ICollection children = (this._occasionalFields == null) ? null : ((ICollection) this._occasionalFIElds.Controls);
this.RenderChildrenInternal(writer, children);
}
一般流程和ASP.Net AJax 1.0分歧就在這裡了
internal void RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
{
//是ASP.Net AJax處理
if ((this.RareFields != null) && (this.RareFIElds.RenderMethod != null))
{
writer.BeginRender();
this.RareFIElds.RenderMethod(writer, this);
writer.EndRender();
}
else if (children != null)
{
//不是ASP.Net AJax處理,將執行foreach依次對所有的子控件進行遍歷
foreach (Control control in children)
{
control.RenderControl(writer);//完成子控件的呈現過程
}
}
}
public virtual void RenderControl(HtmlTextWriter writer)
{
this.RenderControl(writer, this.Adapter);
}
protected void RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
{
if (!this.flags[0x10] && !this.flags[0x200])
{
HttpContext context = (this.Page == null) ? null : this.Page._context;
if ((context != null) && context.TraceIsEnabled)
{
int bufferedLength = context.Response.GetBufferedLength();
this.RenderControlInternal(writer, adapter);
int num2 = context.Response.GetBufferedLength();
context.Trace.AddControlSize(this.UniqueID, num2 - bufferedLength);
}
else
{
this.RenderControlInternal(writer, adapter);
}
}
}private void RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
{
if (adapter != null)
{
adapter.BeginRender(writer);
adapter.Render(writer);
adapter.EndRender(writer);
}
else
{
this.Render(writer);
}
}1.2 WebControl呈現過程
語法
[ThemeableAttribute(true)]
public class WebControl : Control, IAttributeAccessor
說明
1 WebControl.Render方法
(1)大多數情況下,自定義服務器控件開發人員需重寫此方法.
(2)重寫了基類的Render,首先調用WebControl.RenderBeginTag方法,然後調用WebControl.RenderContents方法,最後調用WebControl.RenderEndTag方法來將控件發送到客戶端.
2 WebControl.RenderContents方法
(1)大多數情況下,自定義服務器控件開發人員需重寫此方法.
(2)呈現開始和結束標記之間的控件內容
3 WebControl.RenderBeginTag方法
(1)
4 WebControl.RenderEndTag方法
(1)
1.3 CompositeControl呈現過程
語法
public abstract class CompositeControl : WebControl, INamingContainer, ICompositeControlDesignerAccessor
說明
不得不說的3個方法
1 Control.EnsureChildControls方法
(1)大多數情況下,自定義服務器控件開發人員不需重寫此方法.
(2)確定服務器控件是否包含子控件.如果不包含,則創建子控件.
(3)該方法首先檢查ChildControlsCreated屬性的當前值.如果此值為假,則調用CreateChildControls方法.
2 Control.CreateChildControls方法
(1)大多數情況下,自定義服務器控件開發人員需重寫此方法.
(2)由ASP.Net頁面框架調用,以通知使用基於合成的實現的服務器控件創建它們包含的任何子控件,以便為回發或呈現做准備.
3 CompositeControl.Render方法
(1)大多數情況下,自定義服務器控件開發人員需重寫此方法.
(2)將CompositeControl內容寫入指定的HtmlTextWriter對象,以便在客戶端上顯示.
(3)此方法重寫基實現,以確保先創建Controls集合中的所有子控件,然後使用Control.Render和Control.RenderChildren基方法呈現控件及其所有子控件.