XhtmlTextWriter 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將可延伸超文字標記語言 (XHTML) 特定字元 (包括衍生自 XTHML 的 XHTML 模組所有變異) 寫入 ASP.NET 伺服器控制項的輸出資料流,供行動裝置使用。 請覆寫 XhtmlTextWriter 類別,為 ASP.NET 網頁和伺服器控制項提供自訂 XHTML 呈現。
public ref class XhtmlTextWriter : System::Web::UI::HtmlTextWriter
public class XhtmlTextWriter : System.Web.UI.HtmlTextWriter
type XhtmlTextWriter = class
inherit HtmlTextWriter
Public Class XhtmlTextWriter
Inherits HtmlTextWriter
- 繼承
範例
本節中的程式碼範例包含四個部分。 第一個範例示範如何建立衍生類別。 第二個程式碼範例示範如何建立自訂控制項。 第三個程式碼範例示範如何使用自訂控制項。 第四個程式碼範例提供執行自訂控制項所需的程式碼。
下列程式碼範例示範如何建立衍生自 類別的 XhtmlTextWriter 自訂類別。 它有兩個建構函式,這是直接或間接繼承自 類別之所有類別的標準 HtmlTextWriter 。 第一個建構函式會採用 物件做為參數,並呼叫第二個建構函式,並傳遞下列兩個 TextWriter 參數值:
TextWriter 執行個體。
屬性的值,這個值 HtmlTextWriter.DefaultTabString 會定義 XHTML 文字寫入器所使用的預設行縮排。
此程式碼範例也會示範如何覆寫 OnAttributeRender 和 OnStyleAttributeRender 方法來分別篩選文字大小和色彩樣式。 此外,它會覆寫 BeginRender 和 EndRender 方法,以在控制項轉譯之前和之後寫入文字字串。
using System;
using System.IO;
using System.Web;
using System.Security.Permissions;
using System.Web.UI;
using System.Web.UI.Adapters;
using System.Web.UI.WebControls.Adapters;
namespace Samples.AspNet.CS
{
// Create a class that inherits from XhtmlTextWriter.
[AspNetHostingPermission(SecurityAction.Demand,
Level=AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level=AspNetHostingPermissionLevel.Minimal)]
public class CustomXhtmlTextWriter : XhtmlTextWriter
{
// Create two constructors, following
// the pattern for implementing a
// TextWriter constructor.
public CustomXhtmlTextWriter(TextWriter writer) :
this(writer, DefaultTabString)
{
}
public CustomXhtmlTextWriter(TextWriter writer, string tabString) :
base(writer, tabString)
{
}
// Override the OnAttributeRender method to
// allow this text writer to render only eight-point
// text size.
protected override bool OnAttributeRender(string name,
string value,
HtmlTextWriterAttribute key)
{
if (key == HtmlTextWriterAttribute.Size)
{
if (String.Compare(value, "8pt") == 0)
{
return true;
}
else
{
return false;
}
}
else
{
return base.OnAttributeRender(name, value, key);
}
}
// Override the OnStyleAttributeRender
// method to prevent this text writer
// from rendering purple text.
protected override bool OnStyleAttributeRender(string name,
string value,
HtmlTextWriterStyle key)
{
if (key == HtmlTextWriterStyle.Color)
{
if (String.Compare(value, "purple") == 0)
{
return false;
}
else
{
return true;
}
}
else
{
return base.OnStyleAttributeRender(name, value, key);
}
}
// Override the BeginRender method to write a
// message and call the WriteBreak method
// before a control is rendered.
override public void BeginRender()
{
this.Write("A control is about to render.");
this.WriteBreak();
}
// Override the EndRender method to
// write a string immediately after
// a control has rendered.
override public void EndRender()
{
this.Write("A control just rendered.");
}
}
}
Imports System.IO
Imports System.Web
Imports System.Security.Permissions
Imports System.Web.UI
Imports System.Web.UI.Adapters
Imports System.Web.UI.WebControls.Adapters
Namespace Samples.AspNet.VB
' Create a class that inherits from XhtmlTextWriter.
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class CustomXhtmlTextWriter
Inherits XhtmlTextWriter
' Create two constructors, following
' the pattern for implementing a
' TextWriter constructor.
Public Sub New(writer As TextWriter)
MyClass.New(writer, DefaultTabString)
End Sub
Public Sub New(writer As TextWriter, tabString As String)
MyBase.New(writer, tabString)
End Sub
' Override the OnAttributeRender method to
' allow this text writer to render only eight-point
' text size.
Overrides Protected Function OnAttributeRender(ByVal name As String, _
ByVal value As String, _
ByVal key As HtmlTextWriterAttribute _
) As Boolean
If key = HtmlTextWriterAttribute.Size Then
If String.Compare(value, "8pt") = 0 Then
Return True
Else
Return False
End If
Else
Return MyBase.OnAttributeRender(name, value, key)
End If
End Function
' Override the OnStyleAttributeRender
' method to prevent this text writer
' from rendering purple text.
Overrides Protected Function OnStyleAttributeRender(ByVal name As String, _
ByVal value As String, _
ByVal key As HtmlTextWriterStyle _
) As Boolean
If key = HtmlTextWriterStyle.Color Then
If String.Compare(value, "purple") = 0 Then
Return False
Else
Return True
End If
Else
Return MyBase.OnStyleAttributeRender(name, value, key)
End If
End Function
' Override the BeginRender method to write a
' message and call the WriteBreak method
' before a control is rendered.
Overrides Public Sub BeginRender()
Me.Write("A control is about to render.")
Me.WriteBreak()
End Sub
' Override the EndRender method to
' write a string immediately after
' a control has rendered.
Overrides Public Sub EndRender()
Me.Write("A control just rendered.")
End Sub
End Class
End Namespace
下列程式碼範例示範如何建立名為 的 TestLabel
自訂 Label 控制項,以及名為 XhtmlTestLabelAdapter
的自訂配接器,將控制項的內容轉譯為 XHTML。
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.Adapters;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.Adapters;
namespace AspNet.Samples
{
// Create a simple class that inherits
// from the Label class.
public class TestLabel : Label
{
private String _textValue;
// Override the Text property.
public override string Text
{
get
{
return (string)ViewState["Text"];
}
set
{
ViewState["Text"] = value;
}
}
}
public class XhtmlTestLabelAdapter : WebControlAdapter
{
// Create a control property that accesses the
// methods and properties of the control.
protected TestLabel Control
{
get
{
return (TestLabel)base.Control;
}
}
protected override void Render(HtmlTextWriter writer)
{
// Create an instance of the XhtmlTextWriter class,
// named w, and cast the HtmlTextWriter passed
// in the writer parameter to w.
XhtmlTextWriter w = new XhtmlTextWriter(writer);
// Create a string variable, named value, to hold
// the control's Text property value.
String value = Control.Text;
// Create a Boolean variable, named attTest,
// to test whether the Style attribute is
// valid in the page that the control is
// rendered to.
Boolean attTest = w.IsValidFormAttribute("style");
// Check whether attTest is true or false.
// If true, a style is applied to the XHTML
// content. If false, no style is applied.
if (attTest)
w.EnterStyle(Control.ControlStyle);
// Write the Text property value of the control,
// a <br> element, and a string. Consider encoding the value using WriteEncodedText.
w.Write(value);
w.WriteBreak();
w.Write("This control conditionally rendered its styles for XHTML.");
// Check whether attTest is true or false.
// If true, the XHTML style is closed.
// If false, nothing is rendered.
if (attTest)
w.ExitStyle(Control.ControlStyle);
}
}
}
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.Adapters
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.Adapters
Namespace AspNet.Samples
' Create a simple class that inherits
' from the Label class.
Public Class TestLabel
Inherits Label
Private textValue As String
' Override the Text property.
Overrides Public Property Text As String
Get
Return CStr(ViewState("Text"))
End Get
Set
ViewState("Text") = Value
End Set
End Property
End Class
' Create a class to render the custom Label's
' content to XHTML devices.
Public Class XhtmlTestLabelAdapter
Inherits WebControlAdapter
' Create a Control property that accesses the
' methods and properties of the control.
Protected Shadows ReadOnly Property Control() As TestLabel
Get
Return CType(MyBase.Control, TestLabel)
End Get
End Property
' Override the Render method.
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
' Create an instance of the XhtmlTextWriter class,
' named w, and cast the HtmlTextWriter passed
' in the writer parameter to w.
Dim w As XhtmlTextWriter = New XhtmlTextWriter(writer)
' Create a string variable, named value, to hold
' the control's Text property value.
Dim value As String = Control.Text
' Create a Boolean variable, named attTest,
' to test whether the Style attribute is
' valid in the page that the control is
' rendered to.
Dim attTest As Boolean = w.IsValidFormAttribute("style")
' Check whether attTest is true or false.
' If true, a style is applied to the XHTML
' content. If false, no style is applied.
If (attTest = True) Then
w.EnterStyle(Control.ControlStyle)
End If
' Write the Text property value of the control,
' a <br> element, and a string. Consider encoding the value using WriteEncodedText.
w.Write(value)
w.WriteBreak()
w.Write("This control conditionally rendered its styles for XHTML.")
' Check whether attTest is true or false.
' If true, the XHTML style is closed.
' If false, nothing is rendered.
If (attTest = True) Then
w.ExitStyle(Control.ControlStyle)
End If
End Sub
End Class
End Namespace
下列程式碼範例示範如何在 ASP.NET 網頁上使用自訂控制項 TestLabel
。
<%@ Page Language="C#" %>
<%@ Import Namespace="AspNet.Samples" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
TestLabel tl = new TestLabel();
tl.ID = "TestLabel1";
PlaceHolder1.Controls.Add(tl);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>XHtmlTextWriter Example</title>
</head>
<body>
<form id="form1" runat="server" >
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="AspNet.Samples" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim tl As TestLabel = New TestLabel()
tl.ID = "TestLabel1"
PlaceHolder1.Controls.Add(tl)
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>XHtmlTextWriter Example</title>
</head>
<body>
<form id="form1" runat="server" >
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
若要在上述程式碼範例中使用自訂控制項,請將下列 <controlAdapters>
元素新增至兩個檔案的其中一個。 您可以將它新增至特定瀏覽器子目錄中的適當全電腦檔案,作為.NET Framework組態目錄的子資料夾。 或者,您可以將它新增至 Web 應用程式根目錄下App_Browsers目錄中的自訂瀏覽器檔案。
<controlAdapters>
<adapter controlType="AspNet.Samples.TestLabel"
adapterType="AspNet.Samples.XhtmlTestLabelAdapter" />
</controlAdapters>
備註
XHTML 是以 HTML 4.1 為基礎的 XML 相容標記語言,可讓您建立適合多個裝置類型的網站。 它會將 HTML 所提供的便利性與 XML 所提供的嚴格元素指導方針合併,以產生具有各種格式和樣式選項的標記語言,以及降低的標記標記模棱兩可。 類別 XhtmlTextWriter 提供將 XHTML 內容轉譯至用戶端時,ASP.NET 伺服器控制項使用的格式設定功能。 您可以使用 SetDocType 方法來指定文字寫入器轉譯的 XHTML 類型。 列舉中 XhtmlMobileDocType 定義了支援的檔案類型。
類別 XhtmlTextWriter 會轉譯元素的兩組屬性。 一組是通用屬性的集合,如 屬性中所 CommonAttributes 參考。 第二個集合是元素特定屬性的集合,如 屬性中所 ElementSpecificAttributes 參考。 如需所呈現之元素和樣式的詳細資訊,請參閱 World Wide Web Consortium (W3C) 網站的XHTML 模組化規格。
您可以使用 類別的成員 XhtmlTextWriter 和任何衍生類別來建立自訂文字寫入器,以在自訂 XHTML 頁面配接器或類別配接器中使用。 您也可以建立衍生類別,以覆寫 類別的標準行為 XhtmlTextWriter 。
根據預設,當您使用支援 HTML 4.0 的瀏覽器時,ASP.NET 網頁和控制項轉譯與 XHTML 1.1 標準相容的標記。 如需詳細資訊,請參閱 Visual Studio 和 ASP.NET 中的 XHTML 標準。
除非您特別設定 ASP.NET 不要轉譯 XHTML 標籤,否則輸出 HtmlTextWriter XHTML。 如需詳細資訊,請參閱 如何:在 ASP.NET 網站中設定 XHTML 轉譯。
建構函式
XhtmlTextWriter(TextWriter) |
使用在 XhtmlTextWriter 欄位內指定的行縮排,初始化 DefaultTabString 類別的新執行個體。 如果您不要變更預設的行縮排,請使用 XhtmlTextWriter(TextWriter) 建構函式。 |
XhtmlTextWriter(TextWriter, String) |
使用指定的行縮排,初始化 XhtmlTextWriter 類別的新執行個體。 |
欄位
CoreNewLine |
儲存這個 |
DefaultTabString |
表示單一定位字元。 (繼承來源 HtmlTextWriter) |
DoubleQuoteChar |
表示引號 (") 字元。 (繼承來源 HtmlTextWriter) |
EndTagLeftChars |
表示標記項目結尾標記的左角括弧和斜線符號 (</)。 (繼承來源 HtmlTextWriter) |
EqualsChar |
表示等號 ( |
EqualsDoubleQuoteString |
表示組成字串 (=") 的等號 (=) 和雙引號 (")。 (繼承來源 HtmlTextWriter) |
SelfClosingChars |
表示空格以及標記的自行結尾斜線符號 (/)。 (繼承來源 HtmlTextWriter) |
SelfClosingTagEnd |
表示自我結尾標記項目的結尾斜線符號和右角括弧 (/>)。 (繼承來源 HtmlTextWriter) |
SemicolonChar |
表示分號 (;)。 (繼承來源 HtmlTextWriter) |
SingleQuoteChar |
表示所有格符號 (')。 (繼承來源 HtmlTextWriter) |
SlashChar |
表示斜線符號 (/)。 (繼承來源 HtmlTextWriter) |
SpaceChar |
表示空格 ( ) 字元。 (繼承來源 HtmlTextWriter) |
StyleEqualsChar |
表示樣式等號 ( |
TagLeftChar |
表示標記的開頭角括弧 (<)。 (繼承來源 HtmlTextWriter) |
TagRightChar |
表示標記的結尾角括弧 (>)。 (繼承來源 HtmlTextWriter) |
屬性
CommonAttributes |
取得 Hashtable 物件,其中包含 XhtmlTextWriter 物件之標記 (Markup) 型態之標記 (Tag) 的通用屬性。 |
ElementSpecificAttributes |
取得包含項目專用屬性的 Hashtable 物件。 |
Encoding |
取得 HtmlTextWriter 物件用來將內容寫入網頁的編碼。 (繼承來源 HtmlTextWriter) |
FormatProvider |
取得控制格式設定的物件。 (繼承來源 TextWriter) |
Indent |
取得或設定定位點位置的數目,此為標記的每一行開頭要縮排的數目。 (繼承來源 HtmlTextWriter) |
InnerWriter |
取得或設定寫入標記項目內部內容的文字寫入器。 (繼承來源 HtmlTextWriter) |
NewLine |
取得或設定 HtmlTextWriter 物件所用的行結束字元字串。 (繼承來源 HtmlTextWriter) |
SuppressCommonAttributes |
取得為其隱藏 Hashtable 屬性之項目的 CommonAttributes 物件。 |
TagKey |
取得或設定指定標記項目的 HtmlTextWriterTag 值。 (繼承來源 HtmlTextWriter) |
TagName |
取得或設定所呈現的標記項目的標記名稱。 (繼承來源 HtmlTextWriter) |
方法
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應