Editar

Compartir a través de


EditorPartChrome Class

Definition

Enables developers to override the rendering for only the selected sections of EditorPart controls in an EditorZoneBase zone.

public ref class EditorPartChrome
public class EditorPartChrome
type EditorPartChrome = class
Public Class EditorPartChrome
Inheritance
EditorPartChrome

Examples

The following code example demonstrates how to use the EditorPartChrome class to override the default rendering of EditorPart controls in an EditorZoneBase zone.

The code example has three parts:

  • A user control that enables you to change display modes on a Web Parts page.

  • A Web page that hosts all the controls in the example.

  • A class that contains the source code for a custom EditorPartChrome class and EditorZoneBase zone.

The first part of the code example is the user control. The source code for the user control comes from another topic. For this code example to work, you need to obtain the .ascx file for the user control from the Walkthrough: Changing Display Modes on a Web Parts Page topic, and place the file in the same folder as the .aspx page in this code example.

The second part of the example is the Web page. Note that there is a Register directive near the top of the file to register the compiled component and a tag prefix. Also note that the page references the custom editor zone using the element <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>

The third part of the example contains the implementation of the custom editor part chrome and editor part zone. MyEditorZone extends EditorZone and overrides CreateEditorPartChrome to return the custom editor part chrome. MyEditorPartChrome changes the background color of the editor part control in the CreateEditorPartChromeStyle method. The background color of the zone is changed in the PerformPreRender method, and text is added to the editor part in the RenderPartContents method.

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

Remarks

Chrome refers to the peripheral user interface (UI) elements that frame each Web Parts control or server control contained in a zone. The chrome for a control includes its border, its title bar, and the icons, title text, and verbs menu that appear within the title bar. The appearance of the chrome is set at the zone level, and applies to all the controls in the zone.

The Web Parts control set uses the EditorPartChrome class to render the chrome for EditorPart controls. Additionally, this class provides a way for developers to customize the rendering of any EditorPart controls in an EditorZoneBase zone. For example, you can override the CreateEditorPartChromeStyle method to customize some specific style attributes defined in the EditorZoneBase zone.

The EditorPartChrome class contains several important methods that are useful when you want to override the rendering of EditorPart controls. One is the EditorPartChrome constructor, which you use when you override the CreateEditorPartChrome method in a custom EditorZoneBase class to create an instance of your custom EditorPartChrome object. Another useful method is the RenderPartContents method, which you can use to control the rendering of the content area of controls in a zone (as opposed to chrome elements such as headers, footers, and title bars). Finally, if you want complete programmatic control over all aspects of rendering the EditorPart controls, you can override the RenderEditorPart method.

Notes to Inheritors

If you inherit from the EditorPartChrome class, you must create a customized EditorZone zone to return your customized EditorPartChrome class. The Example section of this class overview demonstrates how to create a customized EditorZone zone to return a customized EditorPartChrome class

Constructors

EditorPartChrome(EditorZoneBase)

Initializes a new instance of the EditorPartChrome class.

Properties

Zone

Gets a reference to the associated EditorZoneBase zone.

Methods

CreateEditorPartChromeStyle(EditorPart, PartChromeType)

Creates the style object that supplies style attributes for each EditorPart control rendered by the EditorPartChrome object.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
PerformPreRender()

Performs tasks that must be done prior to rendering EditorPart controls.

RenderEditorPart(HtmlTextWriter, EditorPart)

Renders a complete EditorPart control with all its sections.

RenderPartContents(HtmlTextWriter, EditorPart)

Renders the main content area of an EditorPart control, excluding the header and footer.

ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to