Page.CreateHtmlTextWriter(TextWriter) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
创建 HtmlTextWriter 对象以呈现该页的内容。
protected:
virtual System::Web::UI::HtmlTextWriter ^ CreateHtmlTextWriter(System::IO::TextWriter ^ tw);
protected public:
virtual System::Web::UI::HtmlTextWriter ^ CreateHtmlTextWriter(System::IO::TextWriter ^ tw);
protected virtual System.Web.UI.HtmlTextWriter CreateHtmlTextWriter (System.IO.TextWriter tw);
protected internal virtual System.Web.UI.HtmlTextWriter CreateHtmlTextWriter (System.IO.TextWriter tw);
abstract member CreateHtmlTextWriter : System.IO.TextWriter -> System.Web.UI.HtmlTextWriter
override this.CreateHtmlTextWriter : System.IO.TextWriter -> System.Web.UI.HtmlTextWriter
Protected Overridable Function CreateHtmlTextWriter (tw As TextWriter) As HtmlTextWriter
Protected Friend Overridable Function CreateHtmlTextWriter (tw As TextWriter) As HtmlTextWriter
参数
- tw
- TextWriter
用于创建 TextWriter 的 HtmlTextWriter。
返回
HtmlTextWriter 或 Html32TextWriter。
示例
下面的代码示例使用 CreateHtmlTextWriter 方法创建名为 MyHtmlTextWriter
的自定义HtmlTextWriter对象的实例。 方法CreateHtmlTextWriter在 派生自 Page的 类中MyPage
被重写,MyHtmlTextWriter
以便在请求页面时呈现 ASP.NET 服务器控件。 请注意,此示例将阻止适配器 TextWriter 行为。
namespace WebPage
{
using System;
using System.IO;
using System.Web.UI;
public class MyPage : Page
{
public MyPage():base()
{
}
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
protected override HtmlTextWriter CreateHtmlTextWriter(TextWriter writer)
{
return new MyHtmlTextWriter(writer);
}
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
protected override void Render(HtmlTextWriter writer)
{
// Write a Font control.
writer.AddAttribute("color", "red");
writer.AddAttribute("size", "6pt");
writer.RenderBeginTag(HtmlTextWriterTag.Font);
writer.Write("<br>" + "The time on the server:<br> " + System.DateTime.Now.ToLongTimeString());
writer.RenderEndTag();
}
}
public class MyHtmlTextWriter : HtmlTextWriter
{
public MyHtmlTextWriter(TextWriter writer):base(writer)
{
writer.Write("<font color=blue> 'MyHtmlTextWriter' is used for rendering.</font>");
}
}
}
Imports System.IO
Imports System.Web.UI
Namespace WebPage
Public Class MyPage
Inherits Page
Public Sub New()
MyBase.New()
End Sub
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Function CreateHtmlTextWriter(ByVal writer As TextWriter) As HtmlTextWriter
Return New MyHtmlTextWriter(writer)
End Function 'CreateHtmlTextWriter
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
' Writes a Font control.
writer.AddAttribute("color", "red")
writer.AddAttribute("size", "6pt")
writer.RenderBeginTag(HtmlTextWriterTag.Font)
writer.Write(("<br>" + "The time on the server:<br> " + System.DateTime.Now.ToLongTimeString()))
writer.RenderEndTag()
End Sub
End Class
Public Class MyHtmlTextWriter
Inherits HtmlTextWriter
Public Sub New(writer As TextWriter)
MyBase.New(writer)
writer.Write("<font color=blue> 'MyHtmlTextWriter' is used for rendering.</font>")
End Sub
End Class
End Namespace 'WebPage
注解
方法CreateHtmlTextWriter通过Browser与页面请求关联的 对象的 Request 属性创建 TextWriter 。 可以在配置部分中添加对 HtmlTextWriter 的 browserCaps
引用。
CreateHtmlTextWriter重写 方法以执行自定义查找。