HtmlTextWriter.AddStyleAttribute Metoda

Definicja

Dodaje atrybut stylu znaczników do tagu otwierającego elementu tworzonego HtmlTextWriter przez obiekt z kolejnym wywołaniem RenderBeginTag metody .

Przeciążenia

AddStyleAttribute(String, String)

Dodaje określony atrybut stylu znaczników i wartość atrybutu do znacznika otwarcia utworzonego przez kolejne wywołanie RenderBeginTag metody .

AddStyleAttribute(HtmlTextWriterStyle, String)

Dodaje atrybut stylu znaczników skojarzony z określoną HtmlTextWriterStyle wartością i wartość atrybutu do tagu znaczników otwierającego utworzonego przez kolejne wywołanie RenderBeginTag metody.

AddStyleAttribute(String, String, HtmlTextWriterStyle)

Dodaje określony atrybut stylu znaczników i wartość atrybutu wraz z HtmlTextWriterStyle wartością wyliczenia do tagu znaczników otwierającego utworzonego przez kolejne wywołanie RenderBeginTag metody.

AddStyleAttribute(String, String)

Dodaje określony atrybut stylu znaczników i wartość atrybutu do znacznika otwarcia utworzonego przez kolejne wywołanie RenderBeginTag metody .

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)

Parametry

name
String

Ciąg zawierający atrybut stylu do dodania.

value
String

Ciąg zawierający wartość do przypisania do atrybutu.

Przykłady

W poniższym przykładzie kodu pokazano, jak używać RenderBeginTag przeciążenia AddStyleAttribute(String, String) metody do renderowania font-size atrybutów stylu i color elementu <p> . W tym przykładzie HtmlTextWriter kodu użyto klasy do renderowania zawartości kontrolki.

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

Uwagi

AddStyleAttribute Użyj przeciążenia AddStyleAttribute(String, String) metody, gdy styl nie jest elementem członkowskim HtmlTextWriterStyle wyliczenia lub nie jest znany do czasu wykonywania.

Klasa HtmlTextWriter utrzymuje listę stylów elementów znaczników renderowanych. Po wywołaniu RenderBeginTag metody wszystkie style dodane przez AddStyleAttribute metodę są renderowane do tagu otwierającego elementu. Lista stylów jest następnie czyszczone.

Wzorzec kodowania elementów znaczników renderowania jest następujący:

  • AddStyleAttribute Użyj metody , aby dodać do elementu wszystkie atrybuty stylu.

  • Użyj metody RenderBeginTag.

  • Użyj innych metod w razie potrzeby, aby renderować zawartość znalezioną między znacznikami otwierania i zamykania elementu.

  • Użyj metody RenderEndTag.

Zobacz też

Dotyczy

AddStyleAttribute(HtmlTextWriterStyle, String)

Dodaje atrybut stylu znaczników skojarzony z określoną HtmlTextWriterStyle wartością i wartość atrybutu do tagu znaczników otwierającego utworzonego przez kolejne wywołanie RenderBeginTag metody.

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)

Parametry

key
HtmlTextWriterStyle

Element HtmlTextWriterStyle reprezentujący atrybut stylu, który ma zostać dodany do strumienia wyjściowego.

value
String

Ciąg zawierający wartość do przypisania do atrybutu.

Przykłady

W poniższym przykładzie kodu pokazano, jak używać części zastąpienia RenderBeginTag metody w klasie pochodzącej HtmlTextWriter z klasy. Kod sprawdza, czy <Label> element jest renderowany. Jeśli tak, metoda jest wywoływana IsStyleAttributeDefined w celu sprawdzenia, czy Color atrybut stylu został zdefiniowany dla <Label> elementu. Color Jeśli atrybut nie został zdefiniowany, kod wywołuje to przeciążenie AddStyleAttribute metody, aby dodać Color atrybut do atrybutu style, a następnie ustawić jego wartość na 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

Uwagi

AddStyleAttribute Użyj przeciążenia AddStyleAttribute(HtmlTextWriterStyle, String) metody, gdy styl jest elementem członkowskim HtmlTextWriterStyle wyliczenia i jest znany przed czasem wykonywania.

Klasa HtmlTextWriter utrzymuje listę stylów elementów znaczników renderowanych. Po wywołaniu RenderBeginTag metody wszystkie style dodane przez AddStyleAttribute metodę są renderowane do tagu otwierającego elementu. Lista stylów jest następnie czyszczone.

Wzorzec kodowania elementów znaczników renderowania jest następujący:

  • AddStyleAttribute Użyj metody , aby dodać do elementu wszystkie atrybuty stylu.

  • Użyj metody RenderBeginTag.

  • Użyj innych metod w razie potrzeby, aby renderować zawartość znalezioną między znacznikami otwierania i zamykania elementu.

  • Użyj metody RenderEndTag.

Zobacz też

Dotyczy

AddStyleAttribute(String, String, HtmlTextWriterStyle)

Dodaje określony atrybut stylu znaczników i wartość atrybutu wraz z HtmlTextWriterStyle wartością wyliczenia do tagu znaczników otwierającego utworzonego przez kolejne wywołanie RenderBeginTag metody.

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)

Parametry

name
String

Ciąg zawierający atrybut stylu do dodania.

value
String

Ciąg zawierający wartość do przypisania do atrybutu.

key
HtmlTextWriterStyle

Element HtmlTextWriterStyle reprezentujący atrybut stylu do dodania.

Uwagi

AddStyleAttribute Użyj przeciążenia AddStyleAttribute(String, String, HtmlTextWriterStyle) metody tylko w przypadku dziedziczenia z HtmlTextWriter klasy. Umożliwia tworzenie nowych name i value par dla HtmlTextWriterStyle atrybutów.

Zobacz też

Dotyczy