This documentation is archived and is not being maintained.

HtmlTextWriter.EnterStyle Method (Style, HtmlTextWriterTag)

Writes the opening tag of a markup element that contains attributes that implement the layout and character formatting of the specified style.

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

public virtual void EnterStyle(
	Style style,
	HtmlTextWriterTag tag


Type: System.Web.UI.WebControls.Style

A Style that specifies the layout and formatting to begin applying to the block of markup.

Type: System.Web.UI.HtmlTextWriterTag

An HtmlTextWriterTag that specifies the opening tag of the markup element that will contain the style object specified in style.

Use the EnterStyle method to apply styles, such as background color or border width, to a block of markup.

The EnterStyle and ExitStyle methods allow a device adapter or control to create markup that uses the character formatting of the specified style. Use the same value for style in the EnterStyle method that you use in the corresponding ExitStyle method.

The EnterStyle overload of the EnterStyle(Style, HtmlTextWriterTag) method renders the opening tag of the element specified by the tag parameter. The EnterStyle(Style, HtmlTextWriterTag) method then adds the necessary attributes and style attributes to the opening tag of the element to display the settings that are specified by the Style object. Use the EnterStyle(Style) overload to render the opening tag of a <span> element.

The following code example demonstrates how to use a custom class named TextSample, derived from the WebControl class, that uses the EnterStyle method to apply a ForeColor style to a string of text.

The EnterStyle method renders the HTML<span style="color:Navy;">. The ExitStyle method call closes the <span> element after the text has been rendered.

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Security.Permissions
Imports System.Drawing

' Create a custom class, named TextSample, that renders 
' its Text property with styles applied by the 
' EnterStyle and ExitStyle methods.  
Namespace AspNet.Samples

    <AspNetHostingPermission(SecurityAction.Demand, _
        Level:=AspNetHostingPermissionLevel.Minimal)> _
    <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
        Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public Class TextSample
        Inherits Control

        ' Create an instance of the Style class. 
        Private textStyle As Style = New Style()
        Private textMessage As String 

        ' Create a Text property. 
        Public Property Text() As String 
                Return textMessage
            End Get 
            Set(ByVal value As String)
                textMessage = value
            End Set 
        End Property 

        Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
            ' Set the value of the Text property.
            textMessage = "Hello, World!" 

            ' Set the Style object's ForeColor 
            ' property to Navy.
            textStyle.ForeColor = Color.Navy

            ' Render the Text property with the style.
            writer.WriteLine("The text property styled: ")

            ' Use the WriteBreak method twice to render 
            ' an empty line between the lines of rendered text.

            ' Render the Text property without the style.
            writer.WriteLine("The Text property unstyled: ")
        End Sub 
    End Class 
End Namespace

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0