語言

EditorPartChrome 類別

定義

允許開發者EditorPartEditorZoneBase覆蓋區域內僅選定控制區段的渲染。

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

範例

以下程式碼範例示範如何利用該EditorPartChrome類別覆蓋區域中控制項EditorPart的預設渲染EditorZoneBase。

該程式碼範例包含三個部分:

  • 一個使用者控制功能,讓你能在網頁部分頁面上更改顯示模式。

  • 一個網頁,承載了範例中所有控制項。

  • 一個包含自訂 EditorPartChrome 類別與 EditorZoneBase 區域原始碼的類別。

程式碼範例的第一部分是使用者控制。 使用者控制的原始碼來自另一個主題。 為了讓這個程式碼範例運作,你需要從「 攻略:更改網頁部分頁面的顯示模式 」主題中取得使用者控制的 .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>

範例的第三部分包含自訂編輯器 part chrome 和編輯器 part zone 的實作。 MyEditorZone 延伸 EditorZone 與覆寫 CreateEditorPartChrome 以返回自訂編輯器部分 Chrome 版本。 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 指的是將每個 Web Parts 控制項或伺服器控制框架於區域內的周邊使用者介面(UI)元素。 控制項的 Chrome 包含其邊框、標題欄,以及標題列內的圖示、標題文字和動詞選單。 鍍鉻的外觀設定在區域層級,並適用於該區域內所有控制。

Web Parts 控制集使用該 EditorPartChrome 類別來渲染控制項的 EditorPart Chrome。 此外,這個類別還提供開發者自訂區域內EditorPart任何EditorZoneBase控制項的渲染方式。 例如,你可以覆寫 CreateEditorPartChromeStyle 該區域中定義 EditorZoneBase 的特定風格屬性來自訂方法。

這個 EditorPartChrome 類別包含幾個重要方法,當你想要覆寫控制項的渲染 EditorPart 時非常有用。 其中一個是EditorPartChrome建構子,當你在自訂CreateEditorPartChrome類別中覆寫該EditorZoneBase方法來建立自訂EditorPartChrome物件的實例時,就會用它。 另一個有用的方法是 RenderPartContents 方法,你可以用它來控制區域控制區的渲染(相對於像標題、頁尾和標題欄這類 Chrome 元素)。 最後,如果你想完全 EditorPart 程式化控制項的所有渲染面向,可以覆寫這個 RenderEditorPart 方法。

給繼承者的注意事項

如果你繼承了該 EditorPartChrome 類別,必須建立自訂 EditorZone 區域才能回傳自訂 EditorPartChrome 類別。 本類別概述的範例部分示範如何建立自訂 EditorZone 區域以回傳自訂 EditorPartChrome 類別

建構函式

名稱 Description
EditorPartChrome(EditorZoneBase)

初始化 EditorPartChrome 類別的新執行個體。

屬性

名稱 Description
Zone

會看到相關 EditorZoneBase 區域的參考。

方法

名稱 Description
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)

適用於