Style 类

表示 Web 服务器控件的样式。

**命名空间:**System.Web.UI.WebControls
**程序集:**System.Web(在 system.web.dll 中)

语法

声明
Public Class Style
    Inherits Component
    Implements IStateManager
用法
Dim instance As Style
public class Style : Component, IStateManager
public ref class Style : public Component, IStateManager
public class Style extends Component implements IStateManager
public class Style extends Component implements IStateManager

备注

Style 类封装控制 Web 服务器控件外观的属性,并可应用于多个 Web 服务器控件以提供通用外观。通过分别设置 BackColorForeColor 属性,可以指定控件的背景色和字体颜色。在可以显示边框的控件上,可以通过设置 BorderWidthBorderStyleBorderColor 属性,控制边框宽度、边框样式和边框颜色。Web 服务器控件的大小还可以用 HeightWidth 属性来指定。

主题 位置
如何:以编程方式设置 ASP.NET 服务器控件样式属性 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:以编程方式设置 ASP.NET 服务器控件样式属性 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:以编程方式设置 ASP.NET 服务器控件样式属性 生成 ASP .NET Web 应用程序

示例

此示例演示如何使用 Style 对象一次更改多个控件的样式属性。每次更改 Style 属性值中的一个值时,各个控件必须调用其 ApplyStyle 方法。注意,并非所有包含的控件都支持演示的所有属性。如果某个控件不支持某个特定的属性,则当该属性值更改时,该控件的外观不会更改。

提示

以下代码示例使用单文件代码模型,当它直接复制到代码隐藏文件时可能不能正常工作。此代码示例必须被复制到具有 .aspx 扩展名的空文本文件中。有关 Web 窗体代码模型的更多信息,请参见 ASP.NET 网页代码模型

<%@ Page language="VB" AutoEventWireup="true" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Data" %>

<html>
    <head>
        <script runat="server">

            Private Dim primaryStyle As New Style()

            Sub Page_Load(sender As Object, e As System.EventArgs)
                If Not Page.IsPostBack Then
                    ' Add data to the borderColorList, 
                    ' backColorList, and foreColorList controls.
                    Dim colors As New ListItemCollection()
                    colors.Add(Color.Black.Name)
                    colors.Add(Color.Blue.Name)
                    colors.Add(Color.Green.Name)
                    colors.Add(Color.Orange.Name)
                    colors.Add(Color.Purple.Name)
                    colors.Add(Color.Red.Name)
                    colors.Add(Color.White.Name)
                    colors.Add(Color.Yellow.Name)
                    borderColorList.DataSource = colors
                    borderColorList.DataBind()        
                    backColorList.DataSource = colors
                    backColorList.DataBind()
                    foreColorList.DataSource = colors
                    foreColorList.DataBind()
                
                    ' Add data to the borderStyleList control.
                    Dim styles As New ListItemCollection()
                    Dim styleType As Type = GetType(BorderStyle)
                    Dim s As String
                    For Each s in [Enum].GetNames(styleType)
                        styles.Add(s)
                    Next s
                    borderStyleList.DataSource = styles
                    borderStyleList.DataBind()  

                    ' Add data to the borderWidthList control.
                    Dim widths As New ListItemCollection()
                    Dim i As Integer
                    for i = 0 To 10
                        widths.Add(i.ToString() & "px")
                    Next i
                    borderWidthList.DataSource = widths
                    borderWidthList.DataBind()

                    ' Add data to the fontNameList control.
                    Dim names As New ListItemCollection()
                    names.Add("Arial")
                    names.Add("Courier")
                    names.Add("Garamond")
                    names.Add("Time New Roman")
                    names.Add("Verdana")
                    fontNameList.DataSource = names
                    fontNameList.DataBind()

                    ' Add data to the fontSizeList control.
                    Dim fontSizes As New ListItemCollection()
                    fontSizes.Add("Small")
                    fontSizes.Add("Medium")
                    fontSizes.Add("Large")
                    fontSizes.Add("10pt")
                    fontSizes.Add("14pt")
                    fontSizes.Add("20pt")
                    fontSizeList.DataSource = fontSizes
                    fontSizeList.DataBind()
                    
                    ' Set primaryStyle as the style for each control.
                    Label1.ApplyStyle(primaryStyle)
                    ListBox1.ApplyStyle(primaryStyle)
                    Button1.ApplyStyle(primaryStyle)
                    Table1.ApplyStyle(primaryStyle)
                    TextBox1.ApplyStyle(primaryStyle)
                End If
            End Sub

            Sub ChangeBorderColor(sender As Object, e As System.EventArgs)
                primaryStyle.BorderColor = _
                    Color.FromName(borderColorList.SelectedItem.Text)
                Label1.ApplyStyle(primaryStyle)
                ListBox1.ApplyStyle(primaryStyle)
                Button1.ApplyStyle(primaryStyle)
                Table1.ApplyStyle(primaryStyle)
                TextBox1.ApplyStyle(primaryStyle)
            End Sub

            Sub ChangeBackColor(sender As Object, e As System.EventArgs)
                primaryStyle.BackColor = _
                    Color.FromName(backColorList.SelectedItem.Text)
                Label1.ApplyStyle(primaryStyle)
                ListBox1.ApplyStyle(primaryStyle)
                Button1.ApplyStyle(primaryStyle)
                Table1.ApplyStyle(primaryStyle)
                TextBox1.ApplyStyle(primaryStyle)
            End Sub

            Sub ChangeForeColor(sender As Object, e As System.EventArgs)
                primaryStyle.ForeColor = _
                    Color.FromName(foreColorList.SelectedItem.Text)
                Label1.ApplyStyle(primaryStyle)
                ListBox1.ApplyStyle(primaryStyle)
                Button1.ApplyStyle(primaryStyle)
                Table1.ApplyStyle(primaryStyle)
                TextBox1.ApplyStyle(primaryStyle)
            End Sub

            Sub ChangeBorderStyle(sender As Object, e As System.EventArgs)
                primaryStyle.BorderStyle = _
                    CType([Enum].Parse(GetType(BorderStyle), _
                    borderStyleList.SelectedItem.Text), BorderStyle)
                Label1.ApplyStyle(primaryStyle)
                ListBox1.ApplyStyle(primaryStyle)
                Button1.ApplyStyle(primaryStyle)
                Table1.ApplyStyle(primaryStyle)
                TextBox1.ApplyStyle(primaryStyle)
            End Sub

            Sub ChangeBorderWidth(sender As Object, e As System.EventArgs)
                primaryStyle.BorderWidth = _
                    Unit.Parse(borderWidthList.SelectedItem.Text)
                Label1.ApplyStyle(primaryStyle)
                ListBox1.ApplyStyle(primaryStyle)
                Button1.ApplyStyle(primaryStyle)
                Table1.ApplyStyle(primaryStyle)
                TextBox1.ApplyStyle(primaryStyle)
            End Sub

            Sub ChangeFont(sender As Object, e As System.EventArgs)
                primaryStyle.Font.Name = _
                    fontNameList.SelectedItem.Text
                Label1.ApplyStyle(primaryStyle)
                ListBox1.ApplyStyle(primaryStyle)
                Button1.ApplyStyle(primaryStyle)
                Table1.ApplyStyle(primaryStyle)
                TextBox1.ApplyStyle(primaryStyle)
            End Sub

            Sub ChangeFontSize(sender As Object, e As System.EventArgs)
                primaryStyle.Font.Size = _
                    FontUnit.Parse(fontSizeList.SelectedItem.Text)
                Label1.ApplyStyle(primaryStyle)
                ListBox1.ApplyStyle(primaryStyle)
                Button1.ApplyStyle(primaryStyle)
                Table1.ApplyStyle(primaryStyle)
                TextBox1.ApplyStyle(primaryStyle)
            End Sub
        </script>
    </head>

    <body>
        <form id="Form1" runat="server">
            <table cellPadding="6" border="0">
                <tr>
                    <td>
                        <asp:label id="Label1" 
                            Text="Border Properties Example" Runat="server">
                            <center><br>Label Styles</center>
                        </asp:label>
                    </td>
                    <td>
                        <asp:button id="Button1" runat="server" 
                            Text="Button Styles">
                        </asp:button>
                    </td>
                    <td>
                        <asp:listbox id="ListBox1" Runat="server">
                            <asp:ListItem Value="0" Text="List Item 0">
                            </asp:ListItem>
                            <asp:ListItem Value="1" Text="List Item 1">
                            </asp:ListItem>
                            <asp:ListItem Value="2" Text="List Item 2">
                            </asp:ListItem>
                        </asp:listbox>
                    </td>
                    <td>
                        <asp:textbox id="TextBox1" 
                            Text="TextBox Styles" Runat="server">
                        </asp:textbox>
                    </td>
                    <td>
                        <asp:table id="Table1" Runat="server">
                            <asp:TableRow>
                                <asp:TableCell Text="(0,0)"></asp:TableCell>
                                <asp:TableCell Text="(0,1)"></asp:TableCell>
                            </asp:TableRow>
                            <asp:TableRow>
                                <asp:TableCell Text="(1,0)"></asp:TableCell>
                                <asp:TableCell Text="(1,1)"></asp:TableCell>
                            </asp:TableRow>
                        </asp:table>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label Runat="server" Text="Border Color:">
                        </asp:Label>
                        <asp:dropdownlist id="borderColorList" 
                            Runat="server" AutoPostBack="True" 
                            OnSelectedIndexChanged="ChangeBorderColor">
                        </asp:dropdownlist>
                        <br>
                        <asp:Label Runat="server" Text="Border Style:">
                        </asp:Label>
                        <asp:dropdownlist id="borderStyleList" 
                            Runat="server" AutoPostBack="True"
                            OnSelectedIndexChanged="ChangeBorderStyle">
                        </asp:dropdownlist>
                        <br>
                        <asp:Label Runat="server" Text="Border Width">
                        </asp:Label>
                        <asp:dropdownlist id="borderWidthList" 
                            Runat="server" AutoPostBack="True"
                            OnSelectedIndexChanged="ChangeBorderWidth">
                        </asp:dropdownlist>
                    </td>
                    <td>
                        <asp:Label Runat="server" Text="Back Color:">
                        </asp:Label>
                        <asp:dropdownlist id="backColorList" 
                            Runat="server" AutoPostBack="True"
                            OnSelectedIndexChanged="ChangeBackColor">
                        </asp:dropdownlist>
                        <br>
                        <asp:Label Runat="server" Text="Foreground Color:">
                        </asp:Label>
                        <asp:dropdownlist id="foreColorList" 
                            Runat="server" AutoPostBack="True"
                            OnSelectedIndexChanged="ChangeForeColor">
                        </asp:dropdownlist>
                    </td>
                    <td>
                        <asp:Label Runat="server" Text="Font Name:">
                        </asp:Label>
                        <asp:dropdownlist id="fontNameList" 
                            Runat="server" AutoPostBack="True"
                            OnSelectedIndexChanged="ChangeFont">
                        </asp:dropdownlist>
                        <br>
                        <asp:Label Runat="server" Text="Font Size:">
                        </asp:Label>
                        <asp:dropdownlist id="fontSizeList" 
                            Runat="server" AutoPostBack="True" 
                            OnSelectedIndexChanged="ChangeFontSize">
                        </asp:dropdownlist>
                    </td>
                </tr>
            </table>

        </form>
    </body>
</html>
<%@ Page language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Data" %>

<html>
    <head>
        <script runat="server">

            private Style primaryStyle = new Style();

            void Page_Load(object sender, System.EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    // Add data to the borderColorList, 
                    // backColorList, and foreColorList controls.
                    ListItemCollection colors = new ListItemCollection();
                    colors.Add(Color.Black.Name);
                    colors.Add(Color.Blue.Name);
                    colors.Add(Color.Green.Name);
                    colors.Add(Color.Orange.Name);
                    colors.Add(Color.Purple.Name);
                    colors.Add(Color.Red.Name);
                    colors.Add(Color.White.Name);
                    colors.Add(Color.Yellow.Name);
                    borderColorList.DataSource = colors;
                    borderColorList.DataBind();         
                    backColorList.DataSource = colors;
                    backColorList.DataBind();
                    foreColorList.DataSource = colors;
                    foreColorList.DataBind();

                    // Add data to the borderStyleList control.
                    ListItemCollection styles = new ListItemCollection();
                    Type styleType = typeof(BorderStyle);
                    foreach (string s in Enum.GetNames(styleType))
                    {
                        styles.Add(s);
                    }
                    borderStyleList.DataSource = styles;
                    borderStyleList.DataBind();  

                    // Add data to the borderWidthList control.
                    ListItemCollection widths = new ListItemCollection();
                    for (int i = 0; i < 11; i++)
                    {
                        widths.Add(i.ToString() + "px");
                    }
                    borderWidthList.DataSource = widths;
                    borderWidthList.DataBind();

                    // Add data to the fontNameList control.
                    ListItemCollection names = new ListItemCollection();
                    names.Add("Arial");
                    names.Add("Courier");
                    names.Add("Garamond");
                    names.Add("Time New Roman");
                    names.Add("Verdana");
                    fontNameList.DataSource = names;
                    fontNameList.DataBind();

                    // Add data to the fontSizeList control.
                    ListItemCollection fontSizes = new ListItemCollection();
                    fontSizes.Add("Small");
                    fontSizes.Add("Medium");
                    fontSizes.Add("Large");
                    fontSizes.Add("10pt");
                    fontSizes.Add("14pt");
                    fontSizes.Add("20pt");
                    fontSizeList.DataSource = fontSizes;
                    fontSizeList.DataBind();
                    
                    //Set primaryStyle as the style for each control.
                    Label1.ApplyStyle(primaryStyle);
                    ListBox1.ApplyStyle(primaryStyle);
                    Button1.ApplyStyle(primaryStyle);
                    Table1.ApplyStyle(primaryStyle);
                    TextBox1.ApplyStyle(primaryStyle);
                }
            }
            void ChangeBorderColor(object sender, System.EventArgs e)
            {
                primaryStyle.BorderColor = 
                    Color.FromName(borderColorList.SelectedItem.Text);
                Label1.ApplyStyle(primaryStyle);
                ListBox1.ApplyStyle(primaryStyle);
                Button1.ApplyStyle(primaryStyle);
                Table1.ApplyStyle(primaryStyle);
                TextBox1.ApplyStyle(primaryStyle);
            }
    
            void ChangeBackColor(object sender, System.EventArgs e)
            {
                primaryStyle.BackColor = 
                    Color.FromName(backColorList.SelectedItem.Text);
                Label1.ApplyStyle(primaryStyle);
                ListBox1.ApplyStyle(primaryStyle);
                Button1.ApplyStyle(primaryStyle);
                Table1.ApplyStyle(primaryStyle);
                TextBox1.ApplyStyle(primaryStyle);
            }

            void ChangeForeColor(object sender, System.EventArgs e)
            {
                primaryStyle.ForeColor = 
                    Color.FromName(foreColorList.SelectedItem.Text);
                Label1.ApplyStyle(primaryStyle);
                ListBox1.ApplyStyle(primaryStyle);
                Button1.ApplyStyle(primaryStyle);
                Table1.ApplyStyle(primaryStyle);
                TextBox1.ApplyStyle(primaryStyle);
            }

            void ChangeBorderStyle(object sender, System.EventArgs e)
            {
                primaryStyle.BorderStyle = 
                    (BorderStyle)Enum.Parse(typeof(BorderStyle), 
                    borderStyleList.SelectedItem.Text);
                Label1.ApplyStyle(primaryStyle);
                ListBox1.ApplyStyle(primaryStyle);
                Button1.ApplyStyle(primaryStyle);
                Table1.ApplyStyle(primaryStyle);
                TextBox1.ApplyStyle(primaryStyle);
            }

            void ChangeBorderWidth(object sender, System.EventArgs e)
            {
                primaryStyle.BorderWidth = 
                    Unit.Parse(borderWidthList.SelectedItem.Text);
                Label1.ApplyStyle(primaryStyle);
                ListBox1.ApplyStyle(primaryStyle);
                Button1.ApplyStyle(primaryStyle);
                Table1.ApplyStyle(primaryStyle);
                TextBox1.ApplyStyle(primaryStyle);
            }

            void ChangeFont(object sender, System.EventArgs e)
            {
                primaryStyle.Font.Name = 
                    fontNameList.SelectedItem.Text;
                Label1.ApplyStyle(primaryStyle);
                ListBox1.ApplyStyle(primaryStyle);
                Button1.ApplyStyle(primaryStyle);
                Table1.ApplyStyle(primaryStyle);
                TextBox1.ApplyStyle(primaryStyle);
            }

            void ChangeFontSize(object sender, System.EventArgs e)
            {
                primaryStyle.Font.Size = 
                    FontUnit.Parse(fontSizeList.SelectedItem.Text);
                Label1.ApplyStyle(primaryStyle);
                ListBox1.ApplyStyle(primaryStyle);
                Button1.ApplyStyle(primaryStyle);
                Table1.ApplyStyle(primaryStyle);
                TextBox1.ApplyStyle(primaryStyle);
            }
        </script>
    </head>

    <body>
        <form id="Form1" runat="server">
            <table cellPadding="6" border="0">
                <tr>
                    <td>
                        <asp:label id="Label1" 
                            Text="Border Properties Example" Runat="server">
                            <center><br>Label Styles</center>
                        </asp:label>
                    </td>
                    <td>
                        <asp:button id="Button1" runat="server" 
                            Text="Button Styles">
                        </asp:button>
                    </td>
                    <td>
                        <asp:listbox id="ListBox1" Runat="server">
                            <asp:ListItem Value="0" Text="List Item 0">
                            </asp:ListItem>
                            <asp:ListItem Value="1" Text="List Item 1">
                            </asp:ListItem>
                            <asp:ListItem Value="2" Text="List Item 2">
                            </asp:ListItem>
                        </asp:listbox>
                    </td>
                    <td>
                        <asp:textbox id="TextBox1" 
                            Text="TextBox Styles" Runat="server">
                        </asp:textbox>
                    </td>
                    <td>
                        <asp:table id="Table1" Runat="server">
                            <asp:TableRow>
                                <asp:TableCell Text="(0,0)"></asp:TableCell>
                                <asp:TableCell Text="(0,1)"></asp:TableCell>
                            </asp:TableRow>
                            <asp:TableRow>
                                <asp:TableCell Text="(1,0)"></asp:TableCell>
                                <asp:TableCell Text="(1,1)"></asp:TableCell>
                            </asp:TableRow>
                        </asp:table>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label Runat="server" Text="Border Color:">
                        </asp:Label>
                        <asp:dropdownlist id="borderColorList" 
                            Runat="server" AutoPostBack="True" 
                            OnSelectedIndexChanged="ChangeBorderColor">
                        </asp:dropdownlist>
                        <br>
                        <asp:Label Runat="server" Text="Border Style:">
                        </asp:Label>
                        <asp:dropdownlist id="borderStyleList" 
                            Runat="server" AutoPostBack="True"
                            OnSelectedIndexChanged="ChangeBorderStyle">
                        </asp:dropdownlist>
                        <br>
                        <asp:Label Runat="server" Text="Border Width">
                        </asp:Label>
                        <asp:dropdownlist id="borderWidthList" 
                            Runat="server" AutoPostBack="True"
                            OnSelectedIndexChanged="ChangeBorderWidth">
                        </asp:dropdownlist>
                    </td>
                    <td>
                        <asp:Label Runat="server" Text="Back Color:">
                        </asp:Label>
                        <asp:dropdownlist id="backColorList" 
                            Runat="server" AutoPostBack="True"
                            OnSelectedIndexChanged="ChangeBackColor">
                        </asp:dropdownlist>
                        <br>
                        <asp:Label Runat="server" Text="Foreground Color:">
                        </asp:Label>
                        <asp:dropdownlist id="foreColorList" 
                            Runat="server" AutoPostBack="True"
                            OnSelectedIndexChanged="ChangeForeColor">
                        </asp:dropdownlist>
                    </td>
                    <td>
                        <asp:Label Runat="server" Text="Font Name:">
                        </asp:Label>
                        <asp:dropdownlist id="fontNameList" 
                            Runat="server" AutoPostBack="True"
                            OnSelectedIndexChanged="ChangeFont">
                        </asp:dropdownlist>
                        <br>
                        <asp:Label Runat="server" Text="Font Size:">
                        </asp:Label>
                        <asp:dropdownlist id="fontSizeList" 
                            Runat="server" AutoPostBack="True" 
                            OnSelectedIndexChanged="ChangeFontSize">
                        </asp:dropdownlist>
                    </td>
                </tr>
            </table>

        </form>
    </body>
</html>

.NET Framework 安全性

继承层次结构

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
      System.Web.UI.WebControls.Style
         派生类

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

Style 成员
System.Web.UI.WebControls 命名空间
BackColor
ForeColor
BorderWidth
BorderStyle
BorderColor
Height
Width

其他资源

ASP.NET 主题和外观概述
ASP.NET Web 服务器控件和 CSS 样式
ASP.NET Web 服务器控件和浏览器功能