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

要检查的特性名称。

返回

如果特性可以应用于 <form> 元素,则为 true;否则为 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 文档类型是否支持该属性。

适用于