Freigeben über


HtmlTextWriter.GetStyleKey-Methode

Ruft den HtmlTextWriterStyle-Enumerationswert für das angegebene Format ab.

Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)

Syntax

'Declaration
Protected Function GetStyleKey ( _
    styleName As String _
) As HtmlTextWriterStyle
'Usage
Dim styleName As String
Dim returnValue As HtmlTextWriterStyle

returnValue = Me.GetStyleKey(styleName)
protected HtmlTextWriterStyle GetStyleKey (
    string styleName
)
protected:
HtmlTextWriterStyle GetStyleKey (
    String^ styleName
)
protected HtmlTextWriterStyle GetStyleKey (
    String styleName
)
protected function GetStyleKey (
    styleName : String
) : HtmlTextWriterStyle

Parameter

Rückgabewert

Der HtmlTextWriterStyle-Enumerationswert, der styleName entspricht.

Hinweise

Die GetStyleKey-Methode gibt den Wert -1 als HtmlTextWriterStyle-Wert zurück, wenn styleName keinem HtmlTextWriterStyle-Enumerationswert entspricht.

Beispiel

Im folgenden Codebeispiel wird das Überschreiben der RenderBeginTag-Methode in einer Klasse veranschaulicht, die von der HtmlTextWriter-Klasse abgeleitet ist. Die RenderBeginTag-Überschreibung bestimmt, ob ein <label>-Markup gerendert wird. Wenn dies der Fall ist, wird das Element auf ein Color-Attribut geprüft. Wenn kein Color-Attribut definiert wurde, wird die GetStyleKey-Methode als erster Parameter in einem Aufruf der AddStyleAttribute-Methode verwendet, mit dem ein Color-Attribut zu einem <label>-Markupelement hinzugefügt und das Color-Attribut auf red festgelegt wird.

' Override the RenderBeginTag method to check whether
' the tagKey parameter is set to a <label> element
' or a <font> element.   
Public Overloads Overrides Sub RenderBeginTag(ByVal tagKey As HtmlTextWriterTag)
    ' If the tagKey parameter is set to a <label> element
    ' but a color attribute is not defined on the element,
    ' the AddStyleAttribute method adds a color attribute
    ' and sets it to red.
    If tagKey = HtmlTextWriterTag.Label Then
        If Not IsStyleAttributeDefined(HtmlTextWriterStyle.Color) Then
            AddStyleAttribute(GetStyleKey("color"), "red")
        End If
    End If

    ' If the tagKey parameter is set to a <font> element
    ' but a size attribute is not defined on the element,
    ' the AddStyleAttribute method adds a size attribute
    ' and sets it to 30 point. 
    If tagKey = HtmlTextWriterTag.Font Then
        If Not IsAttributeDefined(HtmlTextWriterAttribute.Size) Then
            AddAttribute(GetAttributeKey("size"), "30pt")
        End If
    End If

    ' Call the base class's RenderBeginTag method
    ' to ensure that this custom MarkupTextWriter
    ' includes functionality for all other markup elements.
    MyBase.RenderBeginTag(tagKey)
End Sub
// Override the RenderBeginTag method to check whether
// the tagKey parameter is set to a <label> element
// or a <font> element.
public override void RenderBeginTag(HtmlTextWriterTag tagKey)
{

    // If the tagKey parameter is set to a <label> element
    // but a color attribute is not defined on the element,
    // the AddStyleAttribute method adds a color attribute
    // and sets it to red.
    if (tagKey == HtmlTextWriterTag.Label)
    {
        if (!IsStyleAttributeDefined(HtmlTextWriterStyle.Color))
        {
            AddStyleAttribute(GetStyleKey("color"), "red");
        }
    }
    // If the tagKey parameter is set to a <font> element
    // but a size attribute is not defined on the element,
    // the AddStyleAttribute method adds a size attribute
    // and sets it to 30 point. 
    if (tagKey == HtmlTextWriterTag.Font)
    {
        if (!IsAttributeDefined(HtmlTextWriterAttribute.Size))
        {
            AddAttribute(GetAttributeKey("size"), "30pt");
        }
    }
    // Call the base class's RenderBeginTag method
    // to ensure that this custom MarkupTextWriter
    // includes functionality for all other markup elements.
    base.RenderBeginTag(tagKey);
}
// Override the RenderBeginTag method to check whether
// the tagKey parameter is set to a <label> element
// or a <font> element.   
virtual void RenderBeginTag( HtmlTextWriterTag tagKey ) override
{
   // If the tagKey parameter is set to a <label> element
   // but a color attribute is not defined on the element,
   // the AddStyleAttribute method adds a color attribute
   // and sets it to red.
   if ( tagKey == HtmlTextWriterTag::Label )
   {
      if (  !IsStyleAttributeDefined( HtmlTextWriterStyle::Color ) )
      {
         AddStyleAttribute( GetStyleKey( "color" ), "red" );
      }
   }

   // If the tagKey parameter is set to a <font> element
   // but a size attribute is not defined on the element,
   // the AddStyleAttribute method adds a size attribute
   // and sets it to 30 point.
   if ( tagKey == HtmlTextWriterTag::Font )
   {
      if (  !IsAttributeDefined( HtmlTextWriterAttribute::Size ) )
      {
         AddAttribute( GetAttributeKey( "size" ), "30pt" );
      }
   }

   // Call the base class's RenderBeginTag method
   // to ensure that calling this custom markup writer
   // includes functionality for all other elements.
   __super::RenderBeginTag( tagKey );
}
// Override the RenderBeginTag method to check whether
// the tagKey parameter is set to a <label> element 
// or a <font> element.      
public void RenderBeginTag(HtmlTextWriterTag tagKey)
{
    // If the tagKey parameter is set to a Label element
    // but a Color attribute is not defined on the element,
    // the AddStyleAttribute method call adds a Color attribute
    // and sets it to red.
    if (tagKey.Equals(HtmlTextWriterTag.Label)) {
        if (!(IsStyleAttributeDefined(HtmlTextWriterStyle.Color))) {
            AddStyleAttribute(GetStyleKey("color"), "red");
        }
    }

    // If the tagKey parameter is set to a <font> element
    // but a size attribute is not defined on the element,
    // the AddStyleAttribute method call adds a size attribute
    // and sets it to 30 point. 
    if (tagKey.Equals(HtmlTextWriterTag.Font)) {
        if (!(IsAttributeDefined(HtmlTextWriterAttribute.Size))) {
            AddAttribute(GetAttributeKey("size"), "30pt");
        }
    }

    // Call the base class's RenderBeginTag method
    // to ensure that calling this custom HtmlTextWriter
    // includes functionality for all other HTML elements.
    super.RenderBeginTag(tagKey);
} //RenderBeginTag

Plattformen

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

HtmlTextWriter-Klasse
HtmlTextWriter-Member
System.Web.UI-Namespace
TextWriter
HtmlTextWriterStyle
AddStyleAttribute