Html32TextWriter 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將一系列的 HTML 3.2 特定字元和文字寫入至 ASP.NET 伺服器控制件的輸出數據流。 Html32TextWriter 類別提供格式化功能,ASP.NET 伺服器控件在將 HTML 3.2 內容轉譯至用戶端時使用。
public ref class Html32TextWriter : System::Web::UI::HtmlTextWriter
public class Html32TextWriter : System.Web.UI.HtmlTextWriter
type Html32TextWriter = class
inherit HtmlTextWriter
Public Class Html32TextWriter
Inherits HtmlTextWriter
- 繼承
- 衍生
範例
下列程式代碼範例示範如何使用衍生自 Html32TextWriter 類別的類別,名為 CustomHtml32TextWriter
。
CustomHtml32TextWriter
會建立兩個建構函式,遵循由 HtmlTextWriter 類別所建立的模式,並覆寫 RenderBeforeContent、RenderAfterContent、RenderBeforeTag和 RenderAfterTag 方法。
using System.IO;
using System.Web.UI;
namespace Examples.AspNet
{
public class CustomHtml32TextWriter : Html32TextWriter
{
// Create a constructor for the class
// that takes a TextWriter as a parameter.
public CustomHtml32TextWriter(TextWriter writer)
: this(writer, DefaultTabString)
{
}
// Create a constructor for the class that takes
// a TextWriter and a string as parameters.
public CustomHtml32TextWriter(TextWriter writer, String tabString)
: base(writer, tabString)
{
}
// Override the RenderBeforeContent method to render
// styles before rendering the content of a <th> element.
protected override string RenderBeforeContent()
{
// Check the TagKey property. If its value is
// HtmlTextWriterTag.TH, check the value of the
// SupportsBold property. If true, return the
// opening tag of a <b> element; otherwise, render
// the opening tag of a <font> element with a color
// attribute set to the hexadecimal value for red.
if (TagKey == HtmlTextWriterTag.Th)
{
if (SupportsBold)
return "<b>";
else
return "<font color=\"FF0000\">";
}
// Check whether the element being rendered
// is an <H4> element. If it is, check the
// value of the SupportsItalic property.
// If true, render the opening tag of the <i> element
// prior to the <H4> element's content; otherwise,
// render the opening tag of a <font> element
// with a color attribute set to the hexadecimal
// value for navy blue.
if (TagKey == HtmlTextWriterTag.H4)
{
if (SupportsItalic)
return "<i>";
else
return "<font color=\"000080\">";
}
// Call the base method.
return base.RenderBeforeContent();
}
// Override the RenderAfterContent method to close
// styles opened during the call to the RenderBeforeContent
// method.
protected override string RenderAfterContent()
{
// Check whether the element being rendered is a <th> element.
// If so, and the requesting device supports bold formatting,
// render the closing tag of the <b> element. If not,
// render the closing tag of the <font> element.
if (TagKey == HtmlTextWriterTag.Th)
{
if (SupportsBold)
return "</b>";
else
return "</font>";
}
// Check whether the element being rendered is an <H4>.
// element. If so, and the requesting device supports italic
// formatting, render the closing tag of the <i> element.
// If not, render the closing tag of the <font> element.
if (TagKey == HtmlTextWriterTag.H4)
{
if (SupportsItalic)
return "</i>";
else
return "</font>";
}
// Call the base method
return base.RenderAfterContent();
}
// Override the RenderBeforeTag method to render the
// opening tag of a <small> element to modify the text size of
// any <a> elements that this writer encounters.
protected override string RenderBeforeTag()
{
// Check whether the element being rendered is an
// <a> element. If so, render the opening tag
// of the <small> element; otherwise, call the base method.
if (TagKey == HtmlTextWriterTag.A)
return "<small>";
return base.RenderBeforeTag();
}
// Override the RenderAfterTag method to render
// close any elements opened in the RenderBeforeTag
// method call.
protected override string RenderAfterTag()
{
// Check whether the element being rendered is an
// <a> element. If so, render the closing tag of the
// <small> element; otherwise, call the base method.
if (TagKey == HtmlTextWriterTag.A)
return "</small>";
return base.RenderAfterTag();
}
}
}
' Create a custom HtmlTextWriter class that overrides
' the RenderBeforeContent and RenderAfterContent methods.
Imports System.IO
Imports System.Web.UI
Namespace Examples.AspNet
Public Class CustomHtml32TextWriter
Inherits Html32TextWriter
' Create a constructor for the class
' that takes a TextWriter as a parameter.
Public Sub New(ByVal writer As TextWriter)
Me.New(writer, DefaultTabString)
End Sub
' Create a constructor for the class that takes
' a TextWriter and a string as parameters.
Public Sub New(ByVal writer As TextWriter, ByVal tabString As String)
MyBase.New(writer, tabString)
End Sub
' Override the RenderBeforeContent method to render
' styles before rendering the content of a <th> element.
Protected Overrides Function RenderBeforeContent() As String
' Check the TagKey property. If its value is
' HtmlTextWriterTag.TH, check the value of the
' SupportsBold property. If true, return the
' opening tag of a <b> element; otherwise, render
' the opening tag of a <font> element with a color
' attribute set to the hexadecimal value for red.
If TagKey = HtmlTextWriterTag.Th Then
If (SupportsBold) Then
Return "<b>"
Else
Return "<font color=""FF0000"">"
End If
End If
' Check whether the element being rendered
' is an <H4> element. If it is, check the
' value of the SupportsItalic property.
' If true, render the opening tag of the <i> element
' prior to the <H4> element's content; otherwise,
' render the opening tag of a <font> element
' with a color attribute set to the hexadecimal
' value for navy blue.
If TagKey = HtmlTextWriterTag.H4 Then
If (SupportsItalic) Then
Return "<i>"
Else
Return "<font color=""000080"">"
End If
End If
' Call the base method.
Return MyBase.RenderBeforeContent()
End Function
' Override the RenderAfterContent method to close
' styles opened during the call to the RenderBeforeContent
' method.
Protected Overrides Function RenderAfterContent() As String
' Check whether the element being rendered is a <th> element.
' If so, and the requesting device supports bold formatting,
' render the closing tag of the <b> element. If not,
' render the closing tag of the <font> element.
If TagKey = HtmlTextWriterTag.Th Then
If SupportsBold Then
Return "</b>"
Else
Return "</font>"
End If
End If
' Check whether the element being rendered is an <H4>.
' element. If so, and the requesting device supports italic
' formatting, render the closing tag of the <i> element.
' If not, render the closing tag of the <font> element.
If TagKey = HtmlTextWriterTag.H4 Then
If (SupportsItalic) Then
Return "</i>"
Else
Return "</font>"
End If
End If
' Call the base method.
Return MyBase.RenderAfterContent()
End Function
' Override the RenderBeforeTag method to render the
' opening tag of a <small> element to modify the text size of
' any <a> elements that this writer encounters.
Protected Overrides Function RenderBeforeTag() As String
' Check whether the element being rendered is an
' <a> element. If so, render the opening tag
' of the <small> element; otherwise, call the base method.
If TagKey = HtmlTextWriterTag.A Then
Return "<small>"
End If
Return MyBase.RenderBeforeTag()
End Function
' Override the RenderAfterTag method to render
' close any elements opened in the RenderBeforeTag
' method call.
Protected Overrides Function RenderAfterTag() As String
' Check whether the element being rendered is an
' <a> element. If so, render the closing tag of the
' <small> element; otherwise, call the base method.
If TagKey = HtmlTextWriterTag.A Then
Return "</small>"
End If
Return MyBase.RenderAfterTag()
End Function
End Class
End Namespace
備註
Html32TextWriter 類別是 HtmlTextWriter 類別的替代方法。 它會將 HTML 4.0 樣式屬性轉換成相等的 HTML 3.2 標記和屬性。 它會使用 HTML 資料表標準化屬性的傳播,例如色彩和字型。 ASP.NET 藉由檢查 HttpBrowserCapabilities 類別的 TagWriter 屬性,自動將此類別用於 HTML 3.2 和舊版瀏覽器。 除非您建立以使用 HTML 3.2 標記之裝置為目標的自定義頁面或控件配接器,否則您不需要明確建立 Html32TextWriter 類別的實例。
如需自訂頁面和控件轉譯的詳細資訊,請參閱 逐步解說:開發和使用自定義網頁伺服器控制項。
建構函式
Html32TextWriter(TextWriter) |
初始化 Html32TextWriter 類別的新實例,這個實例會在要求瀏覽器需要行縮排時,使用 DefaultTabString 字段中指定的行縮排。 |
Html32TextWriter(TextWriter, String) |
初始化使用指定行縮排之 Html32TextWriter 類別的新實例。 |
欄位
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) |
屬性
Encoding |
取得 HtmlTextWriter 物件用來將內容寫入頁面的編碼方式。 (繼承來源 HtmlTextWriter) |
FontStack |
取得要轉譯之 HTML 的字型資訊集合。 |
FormatProvider |
取得控制項格式設定的物件。 (繼承來源 TextWriter) |
Indent |
取得或設定要縮排每個標記行開頭的定位點位置數目。 (繼承來源 HtmlTextWriter) |
InnerWriter |
取得或設定寫入標記項目內部內容的文字寫入器。 (繼承來源 HtmlTextWriter) |
NewLine |
取得或設定 HtmlTextWriter 物件所使用的行終止符字串。 (繼承來源 HtmlTextWriter) |
ShouldPerformDivTableSubstitution |
取得或設定布爾值,指出是否要將 |
SupportsBold |
取得或設定布爾值,指出要求裝置是否支援粗體 HTML 文字。 使用 SupportsBold 屬性,有條件地將粗體文字轉譯至 Html32TextWriter 輸出數據流。 |
SupportsItalic |
取得或設定布爾值,指出要求裝置是否支援斜體 HTML 文字。 使用 SupportsItalic 屬性,有條件地將斜體文字轉譯至 Html32TextWriter 輸出數據流。 |
TagKey |
取得或設定指定標記專案的 HtmlTextWriterTag 值。 (繼承來源 HtmlTextWriter) |
TagName |
取得或設定要轉譯之標記項目的標記名稱。 (繼承來源 HtmlTextWriter) |
方法
明確介面實作
IDisposable.Dispose() |
如需此成員的描述,請參閱 Dispose()。 (繼承來源 TextWriter) |