HtmlTextWriter.AddStyleAttribute Método

Definición

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

Sobrecargas

AddStyleAttribute(String, String)

Agrega el valor y el estilo de atributo de marcado especificados a la etiqueta de apertura del elemento que crea una llamada posterior al método RenderBeginTag.

AddStyleAttribute(HtmlTextWriterStyle, String)

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

AddStyleAttribute(String, String, HtmlTextWriterStyle)

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

AddStyleAttribute(String, String)

Agrega el valor y el estilo de atributo de marcado especificados a la etiqueta de apertura del elemento que crea una llamada posterior al método RenderBeginTag.

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 con el valor que se ha de 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 cualquier atributo de estilo al elemento .

  • Utilice el método RenderBeginTag.

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

  • Utilice el método RenderEndTag.

Consulte también

Se aplica a

AddStyleAttribute(HtmlTextWriterStyle, String)

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

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

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

value
String

Cadena con el valor que se ha de 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 de estilo y, a continuación, establece su valor en 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)
{
    // 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 es miembro de la HtmlTextWriterStyle enumeración y se conoce 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 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 cualquier atributo de estilo al elemento .

  • Utilice el método RenderBeginTag.

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

  • Utilice el método RenderEndTag.

Consulte también

Se aplica a

AddStyleAttribute(String, String, HtmlTextWriterStyle)

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

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 con el valor que se ha de asignar al atributo.

key
HtmlTextWriterStyle

HtmlTextWriterStyle que 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 nuevos name pares y value para HtmlTextWriterStyle atributos.

Consulte también

Se aplica a