IStyleSheet.RegisterStyle(Style, IUrlResolutionService) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
由类实现时,它将新的样式规则添加至网页的 <head>
部分中的嵌入样式表。
public:
void RegisterStyle(System::Web::UI::WebControls::Style ^ style, System::Web::UI::IUrlResolutionService ^ urlResolver);
public void RegisterStyle (System.Web.UI.WebControls.Style style, System.Web.UI.IUrlResolutionService urlResolver);
abstract member RegisterStyle : System.Web.UI.WebControls.Style * System.Web.UI.IUrlResolutionService -> unit
Public Sub RegisterStyle (style As Style, urlResolver As IUrlResolutionService)
参数
- style
- Style
要添加到嵌入式样式表中的样式规则。
- urlResolver
- IUrlResolutionService
一个实现了 IUrlResolutionService 的对象,它包含当前位置 (URL) 的上下文信息。
示例
下面的代码示例使用 Header 实现 IStyleSheet 来演示如何创建自定义 Style 对象, labelStyle
然后将它注册到当前位置 (URL) 。 然后,标签 label1
调用 MergeStyle 该方法,以便样式 labelStyle
应用于 label1
标签。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
if (Page.Header != null)
{
// Create a Style object to hold style rules to apply to a Label control.
Style labelStyle = new Style();
labelStyle.ForeColor = System.Drawing.Color.DarkRed;
labelStyle.BorderColor = System.Drawing.Color.DarkBlue;
labelStyle.BorderWidth = 2;
// Register the Style object so that it can be merged with
// the Style object of the controls that use it.
Page.Header.StyleSheet.RegisterStyle(labelStyle, null);
// Merge the labelCssStyle style with the label1 control's
// style settings.
label1.MergeStyle(labelStyle);
label1.Text = "This is what the labelCssStyle looks like.";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="head1" runat="server">
<title>IStyleSheet Example</title>
</head>
<body>
<form id="form1" runat="server">
<h1>IStyleSheet Example</h1>
<asp:Label
id="label1"
runat="server">
</asp:Label>
</form>
</body>
</html>
<%@ page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Page.Header Is Nothing Then
' Create a Style object to hold style rules to apply to a Label control.
Dim labelStyle As Style = New Style()
labelStyle.ForeColor = System.Drawing.Color.DarkRed
labelStyle.BorderColor = System.Drawing.Color.DarkBlue
labelStyle.BorderWidth = 2
' Register the Style object so that it can be merged with
' the Style object of the controls that use it.
Page.Header.StyleSheet.RegisterStyle(labelStyle, Nothing)
' Merge the labelCssStyle style with the label1 control's
' style settings.
label1.MergeStyle(labelStyle)
label1.Text = "This is what the labelCssStyle looks like."
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="head1" runat="server">
<title>IStyleSheet Example</title>
</head>
<body>
<form id="form1" runat="server">
<h1>IStyleSheet Example</h1>
<asp:Label
id="label1"
runat="server">
</asp:Label>
</form>
</body>
</html>
注解
此方法将新的样式规则和RegisteredCssClass属性名称添加到对象节Page中的<head>
嵌入样式表,并将规则与自动生成的样式名称相关联。 对象 Style 使用指定的 urlResolver
参数呈现。
如果 urlResolver
设置为 null
,则使用当前 Page URL。
如果已注册对象 Style ,则不会多次添加该对象。
备注
不支持在异步回发期间以编程方式添加或修改样式。 将 AJAX 功能添加到 ASP.NET 网页时,异步回发会更新页面的区域,而无需更新整个页面。 有关详细信息,请参阅 Microsoft Ajax 概述。