HtmlTextWriter.GetStyleKey Method
.NET Framework 2.0
Obtains the HtmlTextWriterStyle enumeration value for the specified style.
Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)
Assembly: System.Web (in system.web.dll)
protected HtmlTextWriterStyle GetStyleKey ( String styleName )
protected function GetStyleKey ( styleName : String ) : HtmlTextWriterStyle
Not applicable.
Parameters
- styleName
The style attribute for which to obtain the HtmlTextWriterStyle.
Return Value
The HtmlTextWriterStyle enumeration value corresponding to styleName.The following code example demonstrates how to override the RenderBeginTag method in a class derived from the HtmlTextWriter class. The RenderBeginTag override determines whether a <label> markup will be rendered, 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 a call to the AddStyleAttribute method to add a Color attribute to a <label> markup element and set the Color attribute to red.
// 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
Community Additions
ADD
Show: