HtmlTextWriter.GetStyleName(HtmlTextWriterStyle) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得與指定 HtmlTextWriterStyle 列舉值相關的標記風格屬性名稱。
protected:
System::String ^ GetStyleName(System::Web::UI::HtmlTextWriterStyle styleKey);
protected string GetStyleName(System.Web.UI.HtmlTextWriterStyle styleKey);
member this.GetStyleName : System.Web.UI.HtmlTextWriterStyle -> string
Protected Function GetStyleName (styleKey As HtmlTextWriterStyle) As String
參數
- styleKey
- HtmlTextWriterStyle
取得 HtmlTextWriterStyle style 屬性名稱。
傳回
與列舉值HtmlTextWriterStyle相關的styleKey樣式屬性名稱。
範例
以下程式碼範例說明如何檢查元素是否 <label> 正在被渲染。 如果是這樣,它會檢查元素上是否 Color 定義了樣式屬性。 若未定義屬性,AddStyleAttribute則呼叫該方法,GetStyleName並將列舉成員轉換為Color其字串表示以傳遞給方法。AddStyleAttribute
// 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
備註
若 styleKey 不是有效 HtmlTextWriterStyle 物件, GetStyleName 該方法會回傳一個空字串(“”)。