HtmlTextWriter.AddStyleAttribute HtmlTextWriter.AddStyleAttribute HtmlTextWriter.AddStyleAttribute HtmlTextWriter.AddStyleAttribute Method

定義

將標記樣式屬性加入項目的開頭標記中,此項目是 HtmlTextWriter 物件經過後續呼叫 RenderBeginTag 方法所建立的。Adds a markup style attribute to the opening tag of the element that the HtmlTextWriter object creates with a subsequent call to the RenderBeginTag method.

多載

AddStyleAttribute(String, String) AddStyleAttribute(String, String) AddStyleAttribute(String, String) AddStyleAttribute(String, String)

將指定的標記樣式屬性和屬性值加入由對 RenderBeginTag 方法的後續呼叫所建立的開頭標記中。Adds the specified markup style attribute and the attribute value to the opening markup tag created by a subsequent call to the RenderBeginTag method.

AddStyleAttribute(HtmlTextWriterStyle, String) AddStyleAttribute(HtmlTextWriterStyle, String) AddStyleAttribute(HtmlTextWriterStyle, String) AddStyleAttribute(HtmlTextWriterStyle, String)

將與指定的 HtmlTextWriterStyle 值相關聯的標記樣式屬性以及屬性值,加入由對 RenderBeginTag 方法的後續呼叫所建立的開頭標記中。Adds the markup style attribute associated with the specified HtmlTextWriterStyle value and the attribute value to the opening markup tag created by a subsequent call to the RenderBeginTag method.

AddStyleAttribute(String, String, HtmlTextWriterStyle) AddStyleAttribute(String, String, HtmlTextWriterStyle) AddStyleAttribute(String, String, HtmlTextWriterStyle) AddStyleAttribute(String, String, HtmlTextWriterStyle)

將指定的標記樣式屬性和屬性值,連同 HtmlTextWriterStyle 列舉值,一起加入由對 RenderBeginTag 方法的後續呼叫所建立的開頭標記中。Adds the specified markup style attribute and the attribute value, along with an HtmlTextWriterStyle enumeration value, to the opening markup tag created by a subsequent call to the RenderBeginTag method.

AddStyleAttribute(String, String) AddStyleAttribute(String, String) AddStyleAttribute(String, String) AddStyleAttribute(String, String)

將指定的標記樣式屬性和屬性值加入由對 RenderBeginTag 方法的後續呼叫所建立的開頭標記中。Adds the specified markup style attribute and the attribute value to the opening markup tag created by a subsequent call to the RenderBeginTag method.

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)

參數

name
String String String String

字串,包含所要加入的樣式屬性。A string that contains the style attribute to add.

value
String String String String

字串,包含要指派給屬性的值。A string that contains the value to assign to the attribute.

範例

下列程式碼範例示範RenderBeginTag如何使用AddStyleAttribute(String, String)方法的多載,在專案上font-size <p>呈現color和樣式屬性。The following code example shows how to use the RenderBeginTag overload of the AddStyleAttribute(String, String) method to render font-size and color style attributes on a <p> element. 這個程式碼範例會HtmlTextWriter使用類別來呈現控制項的內容。This code example uses the HtmlTextWriter class to render the contents of the 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()

備註

當樣式AddStyleAttribute AddStyleAttribute(String, String) 不是列舉的成員,或在執行時間之前不知道時,請使用方法HtmlTextWriterStyle的多載。Use the AddStyleAttribute overload of the AddStyleAttribute(String, String) method when the style is not a member of the HtmlTextWriterStyle enumeration or is not known until run time.

HtmlTextWriter類別會維護其所呈現之標記元素的樣式清單。The HtmlTextWriter class maintains a list of styles for the markup elements it renders. 呼叫方法時, AddStyleAttribute方法所加入的任何樣式都會轉譯成專案的開頭標記。 RenderBeginTagWhen the RenderBeginTag method is called, any styles that are added by the AddStyleAttribute method are rendered to the opening tag of the element. 然後會清除樣式清單。The list of styles is then cleared.

呈現標記元素的編碼模式如下:The coding pattern for rendering markup elements is as follows:

  • AddStyleAttribute使用方法,將任何樣式屬性加入至專案。Use the AddStyleAttribute method to add any style attributes to the element.

  • 請使用 RenderBeginTag 方法。Use the RenderBeginTag method.

  • 視需要使用其他方法來轉譯在元素開頭和結束記號之間找到的內容。Use other methods as needed to render the content found between the element opening and closing tags.

  • 請使用 RenderEndTag 方法。Use the RenderEndTag method.

另請參閱

AddStyleAttribute(HtmlTextWriterStyle, String) AddStyleAttribute(HtmlTextWriterStyle, String) AddStyleAttribute(HtmlTextWriterStyle, String) AddStyleAttribute(HtmlTextWriterStyle, String)

將與指定的 HtmlTextWriterStyle 值相關聯的標記樣式屬性以及屬性值,加入由對 RenderBeginTag 方法的後續呼叫所建立的開頭標記中。Adds the markup style attribute associated with the specified HtmlTextWriterStyle value and the attribute value to the opening markup tag created by a subsequent call to the RenderBeginTag method.

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)

參數

key
HtmlTextWriterStyle HtmlTextWriterStyle HtmlTextWriterStyle HtmlTextWriterStyle

HtmlTextWriterStyle,表示要加入輸出資料流的樣式屬性。An HtmlTextWriterStyle that represents the style attribute to add to the output stream.

value
String String String String

字串,包含要指派給屬性的值。A string that contains the value to assign to the attribute.

範例

下列程式碼範例示範如何在衍生RenderBeginTag HtmlTextWriter自類別的類別中,使用方法的部分覆寫。The following code example demonstrates how to use part of an override of the RenderBeginTag method in a class derived from the HtmlTextWriter class. 程式碼會檢查是否<Label>正在呈現專案。The code checks whether a <Label> element is being rendered. 若是如此, IsStyleAttributeDefined則會呼叫方法來檢查Color是否已為<Label>專案定義樣式屬性。If so, the IsStyleAttributeDefined method is called to check whether a Color style attribute has been defined for the <Label> element. 如果尚未定義AddStyleAttribute Color red屬性,程式碼會呼叫方法的這個多載,將屬性加入至 style 屬性,然後將其值設定為。 ColorIf a Color attribute has not been defined, the code calls this overload of the AddStyleAttribute method to add the Color attribute to the style attribute, and then set its value to 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

備註

當樣式AddStyleAttribute AddStyleAttribute(HtmlTextWriterStyle, String)是列舉的成員,而且在執行時間之前已知時,請使用方法的多載。HtmlTextWriterStyleUse the AddStyleAttribute overload of the AddStyleAttribute(HtmlTextWriterStyle, String) method when the style is a member of the HtmlTextWriterStyle enumeration and is known before run time.

HtmlTextWriter類別會維護其所呈現之標記元素的樣式清單。The HtmlTextWriter class maintains a list of styles for the markup elements it renders. 呼叫方法時, AddStyleAttribute方法所加入的任何樣式都會轉譯成專案的開頭標記。 RenderBeginTagWhen the RenderBeginTag method is called, any styles added by the AddStyleAttribute method are rendered to the opening tag of the element. 然後會清除樣式清單。The list of styles is then cleared.

呈現標記元素的編碼模式如下:The coding pattern for rendering markup elements is as follows:

  • AddStyleAttribute使用方法,將任何樣式屬性加入至專案。Use the AddStyleAttribute method to add any style attributes to the element.

  • 請使用 RenderBeginTag 方法。Use the RenderBeginTag method.

  • 視需要使用其他方法來轉譯在元素開頭和結束記號之間找到的內容。Use other methods as needed to render the content found between the element opening and closing tags.

  • 請使用 RenderEndTag 方法。Use the RenderEndTag method.

另請參閱

AddStyleAttribute(String, String, HtmlTextWriterStyle) AddStyleAttribute(String, String, HtmlTextWriterStyle) AddStyleAttribute(String, String, HtmlTextWriterStyle) AddStyleAttribute(String, String, HtmlTextWriterStyle)

將指定的標記樣式屬性和屬性值,連同 HtmlTextWriterStyle 列舉值,一起加入由對 RenderBeginTag 方法的後續呼叫所建立的開頭標記中。Adds the specified markup style attribute and the attribute value, along with an HtmlTextWriterStyle enumeration value, to the opening markup tag created by a subsequent call to the RenderBeginTag method.

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)

參數

name
String String String String

字串,包含所要加入的樣式屬性。A string that contains the style attribute to be added.

value
String String String String

字串,包含要指派給屬性的值。A string that contains the value to assign to the attribute.

key
HtmlTextWriterStyle HtmlTextWriterStyle HtmlTextWriterStyle HtmlTextWriterStyle

HtmlTextWriterStyle,表示所要加入的樣式屬性。An HtmlTextWriterStyle that represents the style attribute to add.

備註

只有在AddStyleAttribute繼承自AddStyleAttribute(String, String, HtmlTextWriterStyle) 類別時,才使用方法的多載。HtmlTextWriterUse the AddStyleAttribute overload of the AddStyleAttribute(String, String, HtmlTextWriterStyle) method only when inheriting from the HtmlTextWriter class. 它可讓您建立新namevalue和成對HtmlTextWriterStyle的屬性。It enables you to create new name and value pairs for HtmlTextWriterStyle attributes.

另請參閱

適用於