Control,主要有以下4个方法用于呈现:
protected internal virtual void Render(HtmlTextWriter writer)
{
this.RenderChildren();
}
protected internal virtual void RenderChildren(HtmlTextWriter writer)
{
//呈现子控件
}
public virtual void RenderControl(HtmlTextWriter writer)
{
//该方法为入口方法
this.RenderControl(writer,this.xxxAdapter);
}
protected void RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
{
if (adapter != null)
{
//调用相关的适配器方法
}
else
{
this.Render(writer);
}
}
整个呈现顺序就是:{
this.RenderChildren();
}
protected internal virtual void RenderChildren(HtmlTextWriter writer)
{
//呈现子控件
}
public virtual void RenderControl(HtmlTextWriter writer)
{
//该方法为入口方法
this.RenderControl(writer,this.xxxAdapter);
}
protected void RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
{
if (adapter != null)
{
//调用相关的适配器方法
}
else
{
this.Render(writer);
}
}
1.RenderControl(HtmlTextWriter writer)
2.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
3.Render(HtmlTextWriter writer)
4.RenderChildren(HtmlTextWriter writer)
概念上理解就是:
1.控件开始呈现
2.控件是否有相关的呈现适配器,有的话就适配器调用适配器的相关呈现方法
3.呈现子控件
4.完成控件呈现
题外话:关于控件适配器我基本上没用过,不过可以学习这个手法,怎么样在一个类中添加新功能而不影响原代码,既OO的基本设计原则之一 优先使用组合。
WebControl:在Control的基础上增加了Style,呈现方面就是在Render(HtmlTextWriter writer) 方法中扩展了三个呈现方法,增加一个属性呈现方法,
伪代码如下:
public virtual void RenderBeginTag(HtmlTextWriter writer)
{
this.AddAttributesToRender(writer);
//呈现Tag开始标记
}
protected override void Render(HtmlTextWriter writer)
{
//重写了Control的Render方法并增加了三个扩展方法
this.RenderBeginTag(writer);
this.RenderContents(writer);
this.RenderEndTag(writer);
}
protected internal virtual void RenderContents(HtmlTextWriter writer)
{
//调用Control的呈现
base.Render(writer);
}
public virtual void RenderEndTag(HtmlTextWriter writer)
{
//呈现结束标记
}
protected virtual void AddAttributesToRender(HtmlTextWriter writer)
{
//呈现Attribute
}
{
this.AddAttributesToRender(writer);
//呈现Tag开始标记
}
protected override void Render(HtmlTextWriter writer)
{
//重写了Control的Render方法并增加了三个扩展方法
this.RenderBeginTag(writer);
this.RenderContents(writer);
this.RenderEndTag(writer);
}
protected internal virtual void RenderContents(HtmlTextWriter writer)
{
//调用Control的呈现
base.Render(writer);
}
public virtual void RenderEndTag(HtmlTextWriter writer)
{
//呈现结束标记
}
protected virtual void AddAttributesToRender(HtmlTextWriter writer)
{
//呈现Attribute
}
CompositeControl 重写了WebControl的Render(HtmlTextWriter writer),增加了设计时支持以创建子控件
protected internal override void Render(HtmlTextWriter writer)
{
//如果在设计时,创建子控件,也就是在设计时增加友好体验
if (DesignMode)
this.EnsureChildControls();
base.Render(writer);
}
{
//如果在设计时,创建子控件,也就是在设计时增加友好体验
if (DesignMode)
this.EnsureChildControls();
base.Render(writer);
}
以上是三个基类控件基本呈现过程,不足的大家补充!!