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 и присвоить ей возвращаемое значение, полученное IsValidFormAttribute в результате вызова метода со значением параметра style. IsValidFormAttribute Если метод возвращает true, стили, связанные с элементом управления, отрисовываются с помощью HtmlTextWriter.EnterStyle методов и HtmlTextWriter.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 запрашивающего устройства.

Применяется к