EditorPartChrome 类

定义

允许开发人员仅重写 EditorPart 区域中 EditorZoneBase 控件的选定部分的呈现。

public ref class EditorPartChrome
public class EditorPartChrome
type EditorPartChrome = class
Public Class EditorPartChrome
继承
EditorPartChrome

示例

下面的代码示例演示如何使用EditorPartChrome类替代区域中控件EditorZoneBase的默认呈现EditorPart

代码示例有三个部分:

  • 一个用户控件,可用于更改Web 部件页上的显示模式。

  • 承载示例中所有控件的网页。

  • 包含自定义 EditorPartChrome 类和 EditorZoneBase 区域的源代码的类。

代码示例的第一部分是用户控件。 用户控件的源代码来自另一个主题。 若要运行此代码示例,需要从“演练:更改Web 部件页面主题上的显示模式”获取用户控件的 .ascx 文件,并将该文件放置在此代码示例中的 .aspx 页面所在的文件夹中。

示例的第二部分是网页。 请注意,文件顶部有一个 Register 指令,用于注册已编译组件和标记前缀。 另请注意,页面使用元素 <aspSample:MyEditorZone>引用自定义编辑器区域。

<%@ Page Language="C#" %>
<%@ register tagprefix="aspSample" 
  Namespace="Samples.AspNet.CS.Controls" %>
<%@ Register TagPrefix="uc1" TagName="DisplayModeMenuCS" Src="~/DisplayModeMenuCS.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
  <title>Web Parts Page</title>
</head>
<body>
  <h1>Web Parts Demonstration Page</h1>
  <form runat="server" id="form1">
<asp:webpartmanager id="WebPartManager1" runat="server" />
<uc1:DisplayModeMenuCS runat="server" ID="DisplayModeMenu" />
  <br />
  <table cellspacing="0" cellpadding="0" border="0">
    <tr>
      <td valign="top">
    <asp:webpartzone id="SideBarZone" runat="server" 
        headertext="Sidebar">
        <zonetemplate>
        </zonetemplate>
      </asp:webpartzone>
      <aspSample:MyEditorZone ID="EditorZone1" runat="server">
      <ZoneTemplate>
        <asp:AppearanceEditorPart ID="AppearanceEditorPart1" 
          runat="server" />
        <asp:LayoutEditorPart ID="LayoutEditorPart1" 
          runat="server" />
      </ZoneTemplate>
    </aspSample:MyEditorZone>
      </td>
      <td valign="top">
    <asp:webpartzone id="MainZone" runat="server" headertext="Main">
         <zonetemplate>
        <asp:label id="contentPart" runat="server" title="Content">
              <h2>Welcome to My Home Page</h2>
              <p>Use links to visit my favorite sites!</p>
            </asp:label>
         </zonetemplate>
       </asp:webpartzone>
      </td>
      <td valign="top">
      </td>
    </tr>
  </table>
  </form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ register tagprefix="aspSample" 
  Namespace="Samples.AspNet.VB.Controls" %>
<%@ Register TagPrefix="uc1" TagName="DisplayModeMenuVB" Src="~/DisplayModeMenuVB.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
  <title>Web Parts Page</title>
</head>
<body>
  <h1>Web Parts Demonstration Page</h1>
  <form runat="server" id="form1">
<asp:webpartmanager id="WebPartManager1" runat="server" />
<uc1:DisplayModeMenuVB runat="server" ID="DisplayModeMenu" />
  <br />
  <table cellspacing="0" cellpadding="0" border="0">
    <tr>
      <td valign="top">
    <asp:webpartzone id="SideBarZone" runat="server" 
        headertext="Sidebar">
        <zonetemplate>
        </zonetemplate>
      </asp:webpartzone>
      <aspSample:MyEditorZone ID="EditorZone1" runat="server">
      <ZoneTemplate>
        <asp:AppearanceEditorPart ID="AppearanceEditorPart1" 
          runat="server" />
        <asp:LayoutEditorPart ID="LayoutEditorPart1" 
          runat="server" />
      </ZoneTemplate>
    </aspSample:MyEditorZone>
      </td>
      <td valign="top">
    <asp:webpartzone id="MainZone" runat="server" headertext="Main">
         <zonetemplate>
        <asp:label id="contentPart" runat="server" title="Content">
              <h2>Welcome to My Home Page</h2>
              <p>Use links to visit my favorite sites!</p>
            </asp:label>
         </zonetemplate>
       </asp:webpartzone>
      </td>
      <td valign="top">
      </td>
    </tr>
  </table>
  </form>
</body>
</html>

本示例的第三部分包含自定义编辑器部件部件部件版式和编辑器部件区域的实现。 MyEditorZoneEditorZone扩展和替代CreateEditorPartChrome以返回自定义编辑器部件部件版式。 MyEditorPartChrome 更改方法中 CreateEditorPartChromeStyle 编辑器部件控件的背景色。 方法中 PerformPreRender 区域的背景色已更改,并将文本添加到方法中的 RenderPartContents 编辑器部件。

namespace Samples.AspNet.CS.Controls
{

    [AspNetHostingPermission(SecurityAction.Demand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    public class MyEditorPartChrome : EditorPartChrome
    {
        public MyEditorPartChrome(EditorZoneBase zone)
            : base(zone)
        {
        }
        
        protected override Style CreateEditorPartChromeStyle(EditorPart editorPart, PartChromeType chromeType)
        {
            Style editorStyle = base.CreateEditorPartChromeStyle(editorPart, chromeType);
            editorStyle.BackColor = Color.Bisque;
            return editorStyle;
        }

        public override void PerformPreRender()
        {
            Style zoneStyle = new Style();
            zoneStyle.BackColor = Color.Cornsilk;

            Zone.Page.Header.StyleSheet.RegisterStyle(zoneStyle, null);
            Zone.MergeStyle(zoneStyle);
        }

        protected override void RenderPartContents(HtmlTextWriter writer, EditorPart editorPart)
        {
            writer.AddStyleAttribute("color", "red");
            writer.RenderBeginTag("p");
            writer.Write("Apply all changes");
            writer.RenderEndTag();
            editorPart.RenderControl(writer);
        }

        public override void RenderEditorPart(HtmlTextWriter writer, EditorPart editorPart)
        {
            base.RenderEditorPart(writer, editorPart);
        }
    }

    [AspNetHostingPermission(SecurityAction.Demand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    public class MyEditorZone : EditorZone
    {
        protected override EditorPartChrome CreateEditorPartChrome()
        {
            return new MyEditorPartChrome(this);
        }
    }
}
Namespace Samples.AspNet.VB.Controls


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

        Public Sub New(ByVal zone As EditorZoneBase)
            MyBase.New(zone)
        End Sub

        Protected Overrides Function CreateEditorPartChromeStyle(ByVal editorPart As System.Web.UI.WebControls.WebParts.EditorPart, ByVal chromeType As System.Web.UI.WebControls.WebParts.PartChromeType) As System.Web.UI.WebControls.Style
            Dim editorStyle As Style
            editorStyle = MyBase.CreateEditorPartChromeStyle(editorPart, chromeType)
            editorStyle.BackColor = Drawing.Color.Bisque
            Return editorStyle
        End Function

        Public Overrides Sub PerformPreRender()
            Dim zoneStyle As Style = New Style
            zoneStyle.BackColor = Drawing.Color.Cornsilk

            Zone.Page.Header.StyleSheet.RegisterStyle(zoneStyle, Nothing)
            Zone.MergeStyle(zoneStyle)
        End Sub

        Protected Overrides Sub RenderPartContents(ByVal writer As System.Web.UI.HtmlTextWriter, ByVal editorPart As System.Web.UI.WebControls.WebParts.EditorPart)
            writer.AddStyleAttribute("color", "red")
            writer.RenderBeginTag("p")
            writer.Write("Apply all changes")
            writer.RenderEndTag()
            editorPart.RenderControl(writer)
        End Sub

        Public Overrides Sub RenderEditorPart(ByVal writer As System.Web.UI.HtmlTextWriter, ByVal editorPart As System.Web.UI.WebControls.WebParts.EditorPart)
            MyBase.RenderEditorPart(writer, editorPart)
        End Sub
    End Class


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

        Protected Overrides Function CreateEditorPartChrome() As System.Web.UI.WebControls.WebParts.EditorPartChrome
            Return New MyEditorPartChrome(Me)
        End Function
    End Class
End Namespace

注解

Chrome 是指外围用户界面 (UI) 元素,这些元素将框架化区域中包含的每个Web 部件控件或服务器控件。 控件的部件版式包括其边框、标题栏以及标题栏中显示的图标、标题文本和谓词菜单。 部件版式的外观在区域级别设置,适用于该区域中的所有控件。

Web 部件控件集使用EditorPartChrome类来呈现控件的EditorPart部件版式。 此外,此类为开发人员提供了一种自定义区域中任何 EditorPart 控件 EditorZoneBase 呈现的方法。 例如,可以重写 CreateEditorPartChromeStyle 该方法以自定义区域中 EditorZoneBase 定义的某些特定样式属性。

EditorPartChrome 类包含几个重要方法,当你想要重写控件的 EditorPart 呈现时,这些方法非常有用。 一个EditorPartChrome是在自定义EditorZoneBase类中重写CreateEditorPartChrome方法以创建自定义EditorPartChrome对象的实例时使用的构造函数。 另一个有用的方法是 RenderPartContents 该方法,可用于控制区域 (控件的内容区域呈现,而不是页眉、页脚和标题栏等部件元素) 。 最后,如果要对呈现 EditorPart 控件的所有方面进行完整的编程控制,则可以替代 RenderEditorPart 该方法。

继承者说明

如果继承自 EditorPartChrome 该类,则必须创建自定义 EditorZone 区域才能返回自定义 EditorPartChrome 类。 此类概述的示例部分演示如何创建自定义 EditorZone 区域以返回自定义 EditorPartChrome

构造函数

EditorPartChrome(EditorZoneBase)

初始化 EditorPartChrome 类的新实例。

属性

Zone

获取对关联的 EditorZoneBase 区域的引用。

方法

CreateEditorPartChromeStyle(EditorPart, PartChromeType)

创建样式对象,该对象为 EditorPart 对象呈现的每个 EditorPartChrome 控件提供样式特性。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
PerformPreRender()

执行在呈现 EditorPart 控件之前必须完成的任务。

RenderEditorPart(HtmlTextWriter, EditorPart)

呈现完整的 EditorPart 控件及其所有部分。

RenderPartContents(HtmlTextWriter, EditorPart)

呈现 EditorPart 控件的主内容区域,不包括页眉和页脚。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于