XhtmlTextWriter.IsValidFormAttribute(String) 方法

定義

檢查 XHTML 屬性,確定該屬性可以呈現在 <form> 項目的開頭標記中。

public:
 override bool IsValidFormAttribute(System::String ^ attributeName);
public override bool IsValidFormAttribute (string attributeName);
override this.IsValidFormAttribute : string -> bool
Public Overrides Function IsValidFormAttribute (attributeName As String) As Boolean

參數

attributeName
String

要進行檢查的屬性名稱。

傳回

如果屬性可以套用至 true 項目則為 <form>,否則為 false

範例

下列程式碼範例是較大範例的一部分,可建立自訂 Label 控制項,以及將控制項內容轉譯為 XHTML 的配接器。

此程式碼範例示範如何建立名為 attTest 的布林值變數,並將其設定為傳回值,以使用參數值 「style」 呼叫 IsValidFormAttribute 方法所產生的傳回值。 IsValidFormAttribute如果方法傳 true 回 ,則與 控制項相關聯的樣式會使用 HtmlTextWriter.EnterStyleHtmlTextWriter.ExitStyle 方法轉譯。 attTest如果值為 false ,則不會轉譯樣式。 相反地,頁面會顯示控制項的文字、 <br/> 方法所 WriteBreak 轉譯的元素,以及通知使用者控制項的 XHTML 內容有條件地轉譯的文字字串。

此程式碼範例是針對 類別提供的較大範例的 XhtmlTextWriter 一部分。

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);
}
' 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

備註

根據要求裝置的 XHTML 檔案類型是否支援此屬性,這個方法對於有條件地轉譯屬性很有用。

適用於