EditorPartChrome 類別

定義

讓開發人員僅覆寫 EditorPart 區域中 EditorZoneBase 或伺服器控制項之選取區段的呈現。

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

範例

下列程式碼範例示範如何使用 EditorPartChrome 類別來覆寫區域中控制項 EditorZoneBase 的預設轉譯 EditorPart

程式碼範例有三個部分:

  • 使用者控制項,可讓您變更Web 組件頁面上的顯示模式。

  • 裝載範例中所有控制項的網頁。

  • 類別,包含自訂 EditorPartChrome 類別和 EditorZoneBase 區域的原始程式碼。

程式碼範例的第一個部分是使用者控制項。 使用者控制項的原始程式碼來自另一個主題。 若要讓此程式碼範例能夠運作,您必須從逐步解說:變更 Web 組件頁面主題上的顯示模式,並將檔案放在與這個程式碼範例中 .aspx 頁面相同的資料夾中,取得使用者控制項的 .ascx 檔案。

範例的第二個部分是網頁。 請注意,檔案頂端有指示 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>

範例的第三個部分包含自訂編輯器元件 Chrome 和編輯器元件區域的實作。 MyEditorZoneEditorZone擴充和覆寫 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 是指周邊使用者介面 (UI) 元素,這些元素會框住區域中每個Web 組件控制項或伺服器控制項。 控制項的 Chrome 包含其框線、標題列,以及出現在標題列內的圖示、標題文字和動詞功能表。 Chrome 的外觀是在區域層級設定,並套用至區域中的所有控制項。

Web 組件控制項集會 EditorPartChrome 使用 類別來呈現控制項的 EditorPart Chrome。 此外,這個類別也提供一種方式,讓開發人員自訂區域中任何 EditorPart 控制項 EditorZoneBase 的轉譯。 例如,您可以覆寫 CreateEditorPartChromeStyle 方法來自訂區域中定義的 EditorZoneBase 一些特定樣式屬性。

類別 EditorPartChrome 包含數個重要方法,當您想要覆寫控制項的 EditorPart 轉譯時,這些方法很有用。 其中一個是 EditorPartChrome 建構函式,當您覆寫 CreateEditorPartChrome 自訂 EditorZoneBase 類別中的 方法以建立自訂 EditorPartChrome 物件的實例時,會使用這個建構函式。 另一個實用的方法是 方法 RenderPartContents ,您可以用來控制區域控制項內容區域的轉譯 (,而不是頁首、頁尾和標題列等 chrome 元素) 。 最後,如果您想要完整以程式設計方式控制轉譯 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)

適用於