ASP.NET控件呈现方法的说明
一般我们撰写ASP.NET控件都是继承Framework 已有三个基类,Control,WebControl,CompositeControl,它们之间的关系大家都很清楚,下面我就各个基类的呈现方法作一些简单的说明:
Control,主要有以下4个方法用于呈现:
protected internal virtual void Render(HtmlTextWriter writer)
{
this.RenderChildren();
}
protected internal virtual void RenderChildren(HtmlTextWriter writer)
{
//呈现子控件
}
![](/Images/OutliningIndicators/None.gif)
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开始标记
}
![](/Images/OutliningIndicators/None.gif)
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);
}
![](/Images/OutliningIndicators/None.gif)
public virtual void RenderEndTag(HtmlTextWriter writer)
{
//呈现结束标记
}
![](/Images/OutliningIndicators/None.gif)
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);
}
以上是三个基类控件基本呈现过程,不足的大家补充!!
Control,主要有以下4个方法用于呈现:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
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) 方法中扩展了三个呈现方法,增加一个属性呈现方法,
伪代码如下:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
CompositeControl 重写了WebControl的Render(HtmlTextWriter writer),增加了设计时支持以创建子控件
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
以上是三个基类控件基本呈现过程,不足的大家补充!!