HtmlTextWriter.AddStyleAttribute Metodo

Definizione

Aggiunge un attributo di stile markup al tag di apertura dell'elemento creato dall'oggetto HtmlTextWriter con una chiamata successiva del metodo RenderBeginTag.

Overload

AddStyleAttribute(String, String)

Aggiunge l'attributo di stile del markup specificato e il valore dell'attributo al tag del markup di apertura creato da una chiamata successiva del metodo RenderBeginTag.

AddStyleAttribute(HtmlTextWriterStyle, String)

Aggiunge l'attributo di stile markup associato al valore HtmlTextWriterStyle specificato e il valore dell'attributo al tag del markup di apertura creato da una chiamata successiva del metodo RenderBeginTag.

AddStyleAttribute(String, String, HtmlTextWriterStyle)

Aggiunge l'attributo di stile del markup specificato e il valore dell'attributo, oltre a un valore di enumerazione HtmlTextWriterStyle, al markup di apertura creato da una chiamata successiva del metodo RenderBeginTag.

AddStyleAttribute(String, String)

Aggiunge l'attributo di stile del markup specificato e il valore dell'attributo al tag del markup di apertura creato da una chiamata successiva del metodo 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)

Parametri

name
String

Stringa contenente l'attributo di stile da aggiungere.

value
String

Stringa contenente il valore da assegnare all'attributo.

Esempio

Nell'esempio di codice seguente viene illustrato come usare l'overload AddStyleAttribute(String, String) del metodo per eseguire il RenderBeginTag rendering font-size e color gli attributi di stile in un <p> elemento. Questo esempio di codice usa la classe per eseguire il HtmlTextWriter rendering del contenuto del controllo.

// 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()

Commenti

Usare l'overload del metodo quando lo AddStyleAttribute stile non è membro dell'enumerazione AddStyleAttribute(String, String) HtmlTextWriterStyle o non è noto fino a quando non viene eseguito.

La HtmlTextWriter classe mantiene un elenco di stili per gli elementi di markup che esegue il rendering. Quando viene chiamato il RenderBeginTag metodo, tutti gli stili aggiunti dal AddStyleAttribute metodo vengono visualizzati nel tag di apertura dell'elemento. L'elenco di stili viene quindi cancellato.

Il modello di codifica per gli elementi di markup di rendering è il seguente:

  • Utilizzare il AddStyleAttribute metodo per aggiungere tutti gli attributi di stile all'elemento.

  • Usare il metodo RenderBeginTag.

  • Usare altri metodi in base alle esigenze per eseguire il rendering del contenuto trovato tra l'apertura dell'elemento e i tag di chiusura.

  • Usare il metodo RenderEndTag.

Vedi anche

Si applica a

AddStyleAttribute(HtmlTextWriterStyle, String)

Aggiunge l'attributo di stile markup associato al valore HtmlTextWriterStyle specificato e il valore dell'attributo al tag del markup di apertura creato da una chiamata successiva del metodo 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)

Parametri

key
HtmlTextWriterStyle

Oggetto HtmlTextWriterStyle che rappresenta l'attributo di stile da aggiungere al flusso di output.

value
String

Stringa contenente il valore da assegnare all'attributo.

Esempio

Nell'esempio RenderBeginTag di codice seguente viene illustrato come usare parte di un override del metodo in una classe derivata dalla HtmlTextWriter classe . Il codice verifica se viene eseguito il rendering di un <Label> elemento. In tal caso, il IsStyleAttributeDefined metodo viene chiamato per verificare se è stato definito un Color attributo di stile per l'elemento <Label> . Se un Color attributo non è stato definito, il codice chiama questo overload del AddStyleAttribute metodo per aggiungere l'attributo all'attributo Color di stile e quindi impostare il relativo valore su 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

Commenti

Usare l'overload AddStyleAttribute AddStyleAttribute(HtmlTextWriterStyle, String) del metodo quando lo stile è membro dell'enumerazione ed è noto prima dell'esecuzione HtmlTextWriterStyle .

La HtmlTextWriter classe mantiene un elenco di stili per gli elementi di markup che esegue il rendering. Quando viene chiamato il RenderBeginTag metodo, tutti gli stili aggiunti dal AddStyleAttribute metodo vengono visualizzati nel tag di apertura dell'elemento. L'elenco di stili viene quindi cancellato.

Il modello di codifica per gli elementi di markup di rendering è il seguente:

  • Utilizzare il AddStyleAttribute metodo per aggiungere tutti gli attributi di stile all'elemento.

  • Usare il metodo RenderBeginTag.

  • Usare altri metodi in base alle esigenze per eseguire il rendering del contenuto trovato tra l'apertura dell'elemento e i tag di chiusura.

  • Usare il metodo RenderEndTag.

Vedi anche

Si applica a

AddStyleAttribute(String, String, HtmlTextWriterStyle)

Aggiunge l'attributo di stile del markup specificato e il valore dell'attributo, oltre a un valore di enumerazione HtmlTextWriterStyle, al markup di apertura creato da una chiamata successiva del metodo 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)

Parametri

name
String

Stringa contenente l'attributo di stile da aggiungere.

value
String

Stringa contenente il valore da assegnare all'attributo.

key
HtmlTextWriterStyle

Oggetto HtmlTextWriterStyle che rappresenta l'attributo di stile da aggiungere.

Commenti

Usare l'overload AddStyleAttribute AddStyleAttribute(String, String, HtmlTextWriterStyle) del metodo solo quando ereditano dalla HtmlTextWriter classe. Consente di creare nuove name coppie e value per HtmlTextWriterStyle gli attributi.

Vedi anche

Si applica a