HtmlTextWriter.AddStyleAttribute 方法

定義

將標記樣式屬性加入項目的開頭標記中,此項目是 HtmlTextWriter 物件經過後續呼叫 RenderBeginTag 方法所建立的。

多載

AddStyleAttribute(String, String)

將指定的標記樣式屬性和屬性值加入由對 RenderBeginTag 方法的後續呼叫所建立的開頭標記中。

AddStyleAttribute(HtmlTextWriterStyle, String)

將與指定的 HtmlTextWriterStyle 值相關聯的標記樣式屬性以及屬性值,加入由對 RenderBeginTag 方法的後續呼叫所建立的開頭標記中。

AddStyleAttribute(String, String, HtmlTextWriterStyle)

將指定的標記樣式屬性和屬性值,連同 HtmlTextWriterStyle 列舉值,一起加入由對 RenderBeginTag 方法的後續呼叫所建立的開頭標記中。

AddStyleAttribute(String, String)

將指定的標記樣式屬性和屬性值加入由對 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)

參數

name
String

字串,包含所要加入的樣式屬性。

value
String

字串,包含要指派給屬性的值。

範例

下列程式碼範例示範如何使用 RenderBeginTag 方法的多 AddStyleAttribute(String, String) 載,在元素上 <p> 呈現 font-sizecolor 樣式屬性。 此程式碼範例會 HtmlTextWriter 使用 類別來呈現 控制項的內容。

// 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當樣式不是列舉的成員 HtmlTextWriterStyle ,或在執行時間之前不知道時,請使用 方法的多載 AddStyleAttribute(String, String)

類別 HtmlTextWriter 會維護其呈現之標記專案的樣式清單。 RenderBeginTag呼叫 方法時,方法新增 AddStyleAttribute 的任何樣式會轉譯為專案的開頭標記。 然後清除樣式清單。

轉譯標記專案的編碼模式如下所示:

  • AddStyleAttribute使用 方法可將任何樣式屬性新增至 專案。

  • 使用 RenderBeginTag 方法。

  • 視需要使用其他方法來呈現在專案開頭和結束記號之間找到的內容。

  • 使用 RenderEndTag 方法。

另請參閱

適用於

AddStyleAttribute(HtmlTextWriterStyle, String)

將與指定的 HtmlTextWriterStyle 值相關聯的標記樣式屬性以及屬性值,加入由對 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)

參數

key
HtmlTextWriterStyle

HtmlTextWriterStyle,表示要加入輸出資料流的樣式屬性。

value
String

字串,包含要指派給屬性的值。

範例

下列程式碼範例示範如何在衍生自 HtmlTextWriter 類別的類別中使用 方法的 RenderBeginTag 覆寫部分。 程式碼會檢查是否 <Label> 正在轉譯專案。 如果是, IsStyleAttributeDefined 則會呼叫 方法來檢查 Color 是否已為 <Label> 專案定義樣式屬性。 Color如果尚未定義屬性,程式碼會呼叫 方法的 AddStyleAttribute 這個多載,將屬性新增 Color 至 style 屬性,然後將其值設定為 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當樣式是列舉的成員 HtmlTextWriterStyle 且在執行時間之前已知時,請使用 方法的多載 AddStyleAttribute(HtmlTextWriterStyle, String)

類別 HtmlTextWriter 會維護其呈現之標記專案的樣式清單。 RenderBeginTag呼叫 方法時,方法新增 AddStyleAttribute 的任何樣式會轉譯為專案的開頭標記。 然後清除樣式清單。

轉譯標記專案的編碼模式如下所示:

  • AddStyleAttribute使用 方法可將任何樣式屬性新增至 專案。

  • 使用 RenderBeginTag 方法。

  • 視需要使用其他方法來呈現在專案開頭和結束記號之間找到的內容。

  • 使用 RenderEndTag 方法。

另請參閱

適用於

AddStyleAttribute(String, String, HtmlTextWriterStyle)

將指定的標記樣式屬性和屬性值,連同 HtmlTextWriterStyle 列舉值,一起加入由對 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)

參數

name
String

字串,包含所要加入的樣式屬性。

value
String

字串,包含要指派給屬性的值。

key
HtmlTextWriterStyle

HtmlTextWriterStyle,表示所要加入的樣式屬性。

備註

AddStyleAttribute只有在繼承自 HtmlTextWriter 類別時, AddStyleAttribute(String, String, HtmlTextWriterStyle) 才使用 方法的多載。 它可讓您為 HtmlTextWriterStyle 屬性建立新的 namevalue 配對。

另請參閱

適用於