HtmlTextWriter.AddStyleAttribute Método

Definición

Agrega un atributo de estilo de marcado a la etiqueta de apertura del elemento que el HtmlTextWriter objeto crea con una llamada posterior al RenderBeginTag método .

Sobrecargas

Nombre Description
AddStyleAttribute(String, String)

Agrega el atributo de estilo de marcado especificado y el valor del atributo a la etiqueta de marcado de apertura creada por una llamada posterior al RenderBeginTag método .

AddStyleAttribute(HtmlTextWriterStyle, String)

Agrega el atributo de estilo de marcado asociado al valor especificado HtmlTextWriterStyle y el valor del atributo a la etiqueta de marcado de apertura creada por una llamada posterior al RenderBeginTag método .

AddStyleAttribute(String, String, HtmlTextWriterStyle)

Agrega el atributo de estilo de marcado especificado y el valor del atributo, junto con un HtmlTextWriterStyle valor de enumeración, a la etiqueta de marcado de apertura creada por una llamada posterior al RenderBeginTag método .

AddStyleAttribute(String, String)

Agrega el atributo de estilo de marcado especificado y el valor del atributo a la etiqueta de marcado de apertura creada por una llamada posterior al RenderBeginTag método .

public:
 virtual void AddStyleAttribute(System::String ^ name, System::String ^ value);
public virtual void AddStyleAttribute(string name, string value);
abstract member AddStyleAttribute : string * string -> unit
override this.AddStyleAttribute : string * string -> unit
Public Overridable Sub AddStyleAttribute (name As String, value As String)

Parámetros

name
String

Cadena que contiene el atributo de estilo que se va a agregar.

value
String

Cadena que contiene el valor que se va a asignar al atributo .

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar la RenderBeginTag sobrecarga del AddStyleAttribute(String, String) método para representar font-size y color aplicar estilo a atributos en un <p> elemento . En este ejemplo de código se usa la HtmlTextWriter clase para representar el contenido del control.

// Add style attribute for 'p'(paragraph) element.
writer->AddStyleAttribute( "font-size", "12pt" );
writer->AddStyleAttribute( "color", "fuchsia" );
// Output the 'p' (paragraph) element with the style attributes.
writer->RenderBeginTag( "p" );
// Output the 'Message' property contents and the time on the server.
writer->Write( String::Concat( Message, "<br>",
   "The time on the server: ",
   System::DateTime::Now.ToLongTimeString() ) );

// Close the element.
writer->RenderEndTag();
// Add style attribute for 'p'(paragraph) element.
writer.AddStyleAttribute("font-size", "12pt");
writer.AddStyleAttribute("color", "fuchsia");
// Output the 'p' (paragraph) element with the style attributes.
writer.RenderBeginTag("p");
// Output the 'Message' property contents and the time on the server.
writer.Write(Message + "<br>" +
    "The time on the server: " +
    System.DateTime.Now.ToLongTimeString());

// Close the element.
writer.RenderEndTag();
'Add style attribute for 'p'(paragraph) element.
writer.AddStyleAttribute("font-size", "12pt")
writer.AddStyleAttribute("color", "fuchsia")

'Output the 'p' (paragraph) element with the style attributes.
writer.RenderBeginTag("p")

'Output the 'Message' property contents and the time on the server.
writer.Write((Message & "<br>" & "The time on the server: " & _
   System.DateTime.Now.ToLongTimeString()))

' Close the element.
writer.RenderEndTag()

Comentarios

Use la AddStyleAttribute sobrecarga del AddStyleAttribute(String, String) método cuando el estilo no sea miembro de la HtmlTextWriterStyle enumeración o no se conozca hasta el tiempo de ejecución.

La HtmlTextWriter clase mantiene una lista de estilos para los elementos de marcado que representa. Cuando se llama al RenderBeginTag método , los estilos agregados por el AddStyleAttribute método se representan en la etiqueta de apertura del elemento. A continuación, se borra la lista de estilos.

El patrón de codificación para representar elementos de marcado es el siguiente:

  • Use el AddStyleAttribute método para agregar atributos de estilo al elemento .

  • Use el RenderBeginTag método .

  • Use otros métodos según sea necesario para representar el contenido encontrado entre las etiquetas de apertura y cierre del elemento.

  • Use el RenderEndTag método .

Consulte también

Se aplica a

AddStyleAttribute(HtmlTextWriterStyle, String)

Agrega el atributo de estilo de marcado asociado al valor especificado HtmlTextWriterStyle y el valor del atributo a la etiqueta de marcado de apertura creada por una llamada posterior al RenderBeginTag método .

public:
 virtual void AddStyleAttribute(System::Web::UI::HtmlTextWriterStyle key, System::String ^ value);
public virtual void AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle key, string value);
abstract member AddStyleAttribute : System.Web.UI.HtmlTextWriterStyle * string -> unit
override this.AddStyleAttribute : System.Web.UI.HtmlTextWriterStyle * string -> unit
Public Overridable Sub AddStyleAttribute (key As HtmlTextWriterStyle, value As String)

Parámetros

key
HtmlTextWriterStyle

que HtmlTextWriterStyle representa el atributo de estilo que se va a agregar al flujo de salida.

value
String

Cadena que contiene el valor que se va a asignar al atributo .

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar parte de una invalidación del RenderBeginTag método en una clase derivada de la HtmlTextWriter clase . El código comprueba si se representa un <Label> elemento. Si es así, se llama al IsStyleAttributeDefined método para comprobar si se ha definido un Color atributo de estilo para el <Label> elemento . Si no se ha definido un Color atributo, el código llama a esta sobrecarga del AddStyleAttribute método para agregar el Color atributo al atributo style y, a continuación, establece su valor reden .

// If the markup element being rendered is a Label,
// render the opening tag of a <Font> element before it.
if ( tagKey == HtmlTextWriterTag::Label )
{
   
   // Check whether a Color style attribute is
   // included on the Label. If not, use the
   // AddStyleAttribute and GetStyleName methods to add one
   // and set its value to red.
   if (  !IsStyleAttributeDefined( HtmlTextWriterStyle::Color ) )
   {
      AddStyleAttribute( GetStyleName( HtmlTextWriterStyle::Color ), "red" );
   }
// If the markup element being rendered is a Label,
// render the opening tag of a Font element before it.
if (tagKey == HtmlTextWriterTag.Label)
{
    // Check whether a Color style attribute is 
    // included on the Label. If not, use the
    // AddStyleAttribute and GetStyleName methods to add one
    // and set its value to red.
    if (!IsStyleAttributeDefined(HtmlTextWriterStyle.Color))
    {
        AddStyleAttribute(GetStyleName(HtmlTextWriterStyle.Color), "red");
    }
' If the markup element being rendered is a Label,
' render the opening tag of a Font element before it.
If tagKey = HtmlTextWriterTag.Label Then
    ' Check whether a Color style attribute is 
    ' included on the Label. If not, use the
    ' AddStyleAttribute and GetStyleName methods to add one
    ' and set its value to red.
    If Not IsStyleAttributeDefined(HtmlTextWriterStyle.Color) Then
        AddStyleAttribute(GetStyleName(HtmlTextWriterStyle.Color), "red")
    End If

Comentarios

Use la AddStyleAttribute sobrecarga del AddStyleAttribute(HtmlTextWriterStyle, String) método cuando el estilo sea miembro de la HtmlTextWriterStyle enumeración y se conozca antes del tiempo de ejecución.

La HtmlTextWriter clase mantiene una lista de estilos para los elementos de marcado que representa. Cuando se llama al RenderBeginTag método , los estilos agregados por el AddStyleAttribute método se representan a la etiqueta de apertura del elemento. A continuación, se borra la lista de estilos.

El patrón de codificación para representar elementos de marcado es el siguiente:

  • Use el AddStyleAttribute método para agregar atributos de estilo al elemento .

  • Use el RenderBeginTag método .

  • Use otros métodos según sea necesario para representar el contenido encontrado entre las etiquetas de apertura y cierre del elemento.

  • Use el RenderEndTag método .

Consulte también

Se aplica a

AddStyleAttribute(String, String, HtmlTextWriterStyle)

Agrega el atributo de estilo de marcado especificado y el valor del atributo, junto con un HtmlTextWriterStyle valor de enumeración, a la etiqueta de marcado de apertura creada por una llamada posterior al RenderBeginTag método .

protected:
 virtual void AddStyleAttribute(System::String ^ name, System::String ^ value, System::Web::UI::HtmlTextWriterStyle key);
protected virtual void AddStyleAttribute(string name, string value, System.Web.UI.HtmlTextWriterStyle key);
abstract member AddStyleAttribute : string * string * System.Web.UI.HtmlTextWriterStyle -> unit
override this.AddStyleAttribute : string * string * System.Web.UI.HtmlTextWriterStyle -> unit
Protected Overridable Sub AddStyleAttribute (name As String, value As String, key As HtmlTextWriterStyle)

Parámetros

name
String

Cadena que contiene el atributo de estilo que se va a agregar.

value
String

Cadena que contiene el valor que se va a asignar al atributo .

key
HtmlTextWriterStyle

que HtmlTextWriterStyle representa el atributo de estilo que se va a agregar.

Comentarios

Use la AddStyleAttribute sobrecarga del AddStyleAttribute(String, String, HtmlTextWriterStyle) método solo al heredar de la HtmlTextWriter clase . Permite crear pares y value nuevos name para HtmlTextWriterStyle atributos.

Consulte también

Se aplica a