HtmlTextWriter.TagKey Property
Gets or sets the HtmlTextWriterTag value for the specified HTML element.
[Visual Basic] Protected Property TagKey As HtmlTextWriterTag [C#] protected HtmlTextWriterTag TagKey {get; set;} [C++] protected: __property HtmlTextWriterTag get_TagKey(); protected: __property void set_TagKey(HtmlTextWriterTag); [JScript] protected function get TagKey() : HtmlTextWriterTag; protected function set TagKey(HtmlTextWriterTag);
Property Value
The HTML element that is having its opening tag rendered.
Exceptions
| Exception Type | Condition |
|---|---|
| ArgumentOutOfRangeException | The property value cannot be set. |
Remarks
This property is of use only to classes that inherit HtmlTextWriter. You should read or set this property only in a call to the RenderBeginTag method; this is the only time it is set to a consistent value.
Example
[Visual Basic, C#, C++] The following example shows a custom HtmlTextWriter class that overrides the RenderBeforeContent method. It uses the value of TagKey property to determine whether a server control using HtmlTextWriter object is rendering an HTML < label > element. If it is, a < font > element with a color attribute set to red is returned to modify the formatting of the label's text.
[Visual Basic] ' Override the RenderBeforeContent method to write ' a font element that applies red to the text in a Label element. <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _ Protected Overrides Function RenderBeforeContent() As String ' Check to determine whether the element being rendered ' is a label element. If so, render the opening tag ' of the font element; otherwise, call the base method. If TagKey = HtmlTextWriterTag.Label Then Return "<font color=""red"">" Else Return MyBase.RenderBeforeContent() End If End Function 'RenderBeforeContent [C#] // Override the RenderBeforeContent method to write // a font element that applies red to the text in a Label element. [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] protected override string RenderBeforeContent() { // Check to determine whether the element being rendered // is a label element. If so, render the opening tag // of the font element; otherwise, call the base method. if(TagKey == HtmlTextWriterTag.Label) { return "<font color=\"red\">"; } else { return base.RenderBeforeContent(); } } [C++] // Override the RenderBeforeContent method to write // a font element that applies red to the text in a Label element. protected: [System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name="FullTrust")] String* RenderBeforeContent() { // Check to determine whether the element being rendered // is a label element. If so, render the opening tag // of the font element; otherwise, call the base method. if (TagKey == HtmlTextWriterTag::Label) { return S"<font color=\"red\">"; } else { return __super::RenderBeforeContent(); } }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
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 | TagName