C# Render
Control.Render 方法
将服务器控件内容发送到提供的 HtmlTextWriter 对象,此对象编写将在客户端呈现的内容。
protected virtual void Render(
HtmlTextWriter writer
);
参数
writer
接收服务器控件内容的 HtmlTextWriter 对象。
备注
在开发自定义服务器控件时,可以重写此方法以生成 ASP.NET 页的内容。有关更多信息,请参见 ASP.NET 服务器控件中的方法。
示例
下面的示例重写 Render 方法,使用 HasControls 方法查看服务器控件是否在其 ControlCollection 对象(可通过 Control.Controls 属性访问)中存储了任何子控件。如果有子控件,它检查集合中的第一个服务器控件是否为文本。如果两个条件都符合,此文本就追加到 HTML 字符串中。
[C#]
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
protected override void Render(HtmlTextWriter output) {
if ( (HasControls()) && (Controls[0] is LiteralControl) ) {
output.Write("<H2>Your Message: " + ((LiteralControl) Controls[0]).Text + "</H2>");
}
}
OnPreRender是在你显示页面内容前可以自己增加显示信息
将服务器控件内容发送到提供的 HtmlTextWriter 对象,此对象编写将在客户端呈现的内容。
protected virtual void Render(
HtmlTextWriter writer
);
参数
writer
接收服务器控件内容的 HtmlTextWriter 对象。
备注
在开发自定义服务器控件时,可以重写此方法以生成 ASP.NET 页的内容。有关更多信息,请参见 ASP.NET 服务器控件中的方法。
示例
下面的示例重写 Render 方法,使用 HasControls 方法查看服务器控件是否在其 ControlCollection 对象(可通过 Control.Controls 属性访问)中存储了任何子控件。如果有子控件,它检查集合中的第一个服务器控件是否为文本。如果两个条件都符合,此文本就追加到 HTML 字符串中。
[C#]
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
protected override void Render(HtmlTextWriter output) {
if ( (HasControls()) && (Controls[0] is LiteralControl) ) {
output.Write("<H2>Your Message: " + ((LiteralControl) Controls[0]).Text + "</H2>");
}
}
二:
OnPreRender是在你显示页面内容前可以自己增加显示信息