This documentation is archived and is not being maintained.

HtmlTextWriter.GetStyleKey Method

Obtains the HtmlTextWriterStyle enumeration value for the specified HTML style.

[Visual Basic]
Protected Function GetStyleKey( _
   ByVal styleName As String _
) As HtmlTextWriterStyle
[C#]
protected HtmlTextWriterStyle GetStyleKey(
 string styleName
);
[C++]
protected: HtmlTextWriterStyle GetStyleKey(
 String* styleName
);
[JScript]
protected function GetStyleKey(
   styleName : String
) : HtmlTextWriterStyle;

Parameters

styleName
The HTML style attribute to obtain the HtmlTextWriterStyle value for.

Return Value

The HtmlTextWriterStyle enumeration value, or HtmlTextWriterStyle.Unknown if the HTML style is not a member of the enumeration.

Example

[Visual Basic, C#, C++] The following example shows a custom HtmlTextWriter class that overrides the RenderBeginTag method. It determines whether an HTML element to be rendered is a < label > element, and, if so, checks the element for a color attribute. If a color attribute has not been defined, the GetStyleKey method is used as the first parameter in an AddStyleAttribute method call to add a color attribute to a < label > HTML element. The second parameter sets the color attribute to red.

[Visual Basic] 
' Override the RenderBeginTag method to check the tagKey
' parameter see whether the tag to render is an HTML label 
' or a font element.

Overloads Overrides Public Sub RenderBeginTag(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 call 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 call 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 calling this custom HtmlTextWriter
   ' includes functionality for all other HTML elements.
   MyBase.RenderBeginTag(tagKey)
End Sub 'RenderBeginTag   

[C#] 
// Override the RenderBeginTag method to check the tagKey
// parameter see whether the tag to render is an HTML label 
// 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 call 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 call 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 HtmlTextWriter
   // includes functionality for all other HTML elements.
   base.RenderBeginTag(tagKey);
}

[C++] 
// Override the RenderBeginTag method to check the tagKey
// parameter see whether the tag to render is an HTML label
// or a font element.

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 == HtmlTextWriterTag::Label) {
      if (!IsStyleAttributeDefined(HtmlTextWriterStyle::Color)) {
         AddStyleAttribute(GetStyleKey(S"color"), S"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 == HtmlTextWriterTag::Font) {
      if (!IsAttributeDefined(HtmlTextWriterAttribute::Size)) {
         AddAttribute(GetAttributeKey(S"size"), S"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);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

HtmlTextWriter Class | HtmlTextWriter Members | System.Web.UI Namespace

Show: