WebPartChrome 类

定义

允许开发人员仅重写 WebPart 区域中 WebPartZoneBase 或服务器控件的选定部分的呈现。Enables developers to override the rendering for only the selected sections of WebPart or server controls in a WebPartZoneBase zone.

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

示例

下面的代码示例演示如何使用 WebPartChrome 类覆盖 WebPartZoneBase 区域中 WebPart 控件的默认呈现。The following code example demonstrates how to use the WebPartChrome class to override the default rendering of WebPart controls in a WebPartZoneBase zone.

此代码示例包含四个部分:The code example has four parts:

  • 使您能够在 Web 部件页上更改显示模式的用户控件。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.

  • 一个包含自定义 WebPartZoneBase 区域和 WebPartChrome 类的源代码的类。A class that contains the source code for a custom WebPartZoneBase zone and a WebPartChrome class.

  • 此示例的工作原理的说明。An explanation of how the example works.

此代码示例的第一部分是用户控件。The first part of the code example is the user control. 该用户控件的源代码来自其他主题。The source code for the user control comes from another topic. 要使此代码示例正常运行,你需要从演练:更改 Web 部件页上的显示模式主题中获取该用户控件的 .ascx 文件,并将该文件放在此代码示例中 .aspx 页面所在的同一文件夹中。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. 请注意,在文件顶部附近有一个 Register 指令,用于注册编译的组件和标记前缀。Note that there is a Register directive near the top of the file to register the compiled component and a tag prefix. 另请注意,页面使用元素 <aspSample:MyZone> 引用自定义区域,并且在区域内包含多个标准 ASP.NET 服务器控件。Also note that the page references the custom zone using the element <aspSample:MyZone> and includes several standard ASP.NET server controls within the zone. 服务器控件不实现任何实际功能;此处仅用于说明 WebPartChrome 类功能适用于呈现区域的方式。The server controls do not implement any actual functionality; they are used here only to illustrate how the WebPartChrome class features apply to rendering zones. 请注意,这些服务器控件(尽管它们不是实际的 WebPart 控件)在运行时由 GenericWebPart 对象自动包装(通过 ASP.NET),因此它们具有与 WebPart 控件相同的功能。Note that these server controls, though they are not actual WebPart controls, are each automatically wrapped (by ASP.NET) with a GenericWebPart object at run time, so they will have the same functionality as WebPart controls.

<%@ Page Language="C#" %>
<%@ Register TagPrefix="uc1" 
  TagName="DisplayModeMenuCS"
  Src="~/DisplayModeMenuCS.ascx" %>
<%@ Register TagPrefix="aspSample" 
  Namespace="Samples.AspNet.CS.Controls" 
  Assembly="MyChromeCS" %>

<!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 runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
  <form id="form1" runat="server">
    <asp:WebPartManager ID="WebPartManager1" runat="server" />
    <uc1:DisplayModeMenuCS id="menu1" runat="server" />
    <aspSample:MyZone ID="WebPartZone1" runat="server"
      RenderVerbsInMenu="true">
      <PartTitleStyle Font-Bold="true"
        BorderWidth="1" 
        BackColor="lightblue"/>
      <ZoneTemplate>
        <asp:Panel runat="server" id="panel1" 
          title="Vote on Issues" >
          <asp:RadioButtonList ID="RadioButtonList1" runat="server" >
            <asp:ListItem Value="1">Issue 1</asp:ListItem>
            <asp:ListItem Value="2">Issue 2</asp:ListItem>
            <asp:ListItem Value="3">Issue 3</asp:ListItem>
          </asp:RadioButtonList>
          <asp:Button ID="Button1" runat="server" Text="Cast Vote" />
        </asp:Panel>
        <asp:FileUpload ID="FileUpload1" runat="server" 
          title="Upload Files" />
      </ZoneTemplate>
    </aspSample:MyZone>
    <asp:WebPartZone ID="WebPartZone2" runat="server" />
    <asp:EditorZone ID="EditorZone1" runat="server">
      <ZoneTemplate>
        <asp:AppearanceEditorPart ID="AppearanceEditorPart1" runat="server" />
      </ZoneTemplate>
    </asp:EditorZone>
  </form>
</body>
</html>
<%@ Page Language="vb" %>
<%@ Register TagPrefix="uc1" 
  TagName="DisplayModeMenuVB"
  Src="~/DisplayModeMenuVB.ascx" %>
<%@ Register TagPrefix="aspSample" 
  Namespace="Samples.AspNet.VB.Controls" 
  Assembly="MyChromeVB" %>

<!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 runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
  <form id="form1" runat="server">
    <asp:WebPartManager ID="WebPartManager1" runat="server" />
    <uc1:DisplayModeMenuVB id="menu1" runat="server" />
    <aspSample:MyZone ID="WebPartZone1" runat="server"
      RenderVerbsInMenu="true">
      <PartTitleStyle Font-Bold="true"
        BorderWidth="1" 
        BackColor="lightblue"/>
      <ZoneTemplate>
        <asp:Panel runat="server" id="panel1" 
          title="Vote on Issues" >
          <asp:RadioButtonList ID="RadioButtonList1" runat="server" >
            <asp:ListItem Value="1">Issue 1</asp:ListItem>
            <asp:ListItem Value="2">Issue 2</asp:ListItem>
            <asp:ListItem Value="3">Issue 3</asp:ListItem>
          </asp:RadioButtonList>
          <asp:Button ID="Button1" runat="server" Text="Cast Vote" />
        </asp:Panel>
        <asp:FileUpload ID="FileUpload1" runat="server" 
          title="Upload Files" />
      </ZoneTemplate>
    </aspSample:MyZone>
    <asp:WebPartZone ID="WebPartZone2" runat="server" />
    <asp:EditorZone ID="EditorZone1" runat="server">
      <ZoneTemplate>
        <asp:AppearanceEditorPart ID="AppearanceEditorPart1" runat="server" />
      </ZoneTemplate>
    </asp:EditorZone>
  </form>
</body>
</html>

此代码示例的第三部分是自定义 WebPartZoneBase 区域和 WebPartChrome 类的源。The third part of the code example is the source for the custom WebPartZoneBase zone and a WebPartChrome class. 在自定义区域类的构造函数中,代码检查 MyZone.RenderVerbsInMenu 属性。In the constructor for the custom zone class, the code checks the MyZone.RenderVerbsInMenu property. 如果 true值,谓词将呈现在区域中每个 WebPart 控件的菜单中。If the value is true, verbs are rendered in a menu for each of the WebPart controls in the zone. 这是 Web 部件控件集中的正常默认行为。This is the normal, default behavior in the Web Parts controls set. 如果 MyZone.RenderVerbsInMenu 属性值为 false(这是此自定义区域中的默认值),则将在每个控件的标题栏中将谓词单独呈现为链接。If the MyZone.RenderVerbsInMenu property value is false, which is the default in this custom zone, the verbs are rendered individually as links in the title bar of each control. 请注意,在声明 <aspSample:MyZone> 元素的网页代码中,RenderVerbsInMenu 属性设置为 true,以便谓词出现在控件上的菜单中。Notice that in the Web page code where the <aspSample:MyZone> element is declared, there is a RenderVerbsInMenu attribute set to true, so that the verbs will appear in menus on the controls. 若要尝试此功能,可以将声明性 RenderVerbsInMenu 特性设置为 false,并注意谓词如何呈现为链接。To experiment with this feature, you can set the declarative RenderVerbsInMenu attribute to false, and note how the verbs are rendered as links.

自定义 WebPartChrome 类在多个方法上覆盖呈现,而自定义区域则在其 CreateWebPartChrome 方法中创建 MyWebPartChrome 类的实例。The custom WebPartChrome class overrides the rendering on several methods, and the custom zone creates an instance of the MyWebPartChrome class in its CreateWebPartChrome method. 这会将自定义呈现应用于页面。This applies the custom rendering to the page. 要使代码示例运行,必须编译此源代码。For the code example to run, you must compile this source code. 可以显式编译该程序集,并将生成的程序集放在网站的 Bin 文件夹或全局程序集缓存中。You can compile it explicitly and put the resulting assembly in your Web site's Bin folder or the global assembly cache. 或者,您可以将源代码放在站点的 App_Code 文件夹中,它将在运行时动态编译。Alternatively, you can put the source code in your site's App_Code folder, where it will be dynamically compiled at run time. 有关演示两种编译方法的演练,请参阅演练:开发和使用自定义 Web 服务器控件For a walkthrough that demonstrates both methods of compiling, see Walkthrough: Developing and Using a Custom Web Server Control.

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Samples.AspNet.CS.Controls
{
  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  public class MyZone : WebPartZone
  {
    private Boolean _renderVerbsInMenu;

    protected override WebPartChrome CreateWebPartChrome()
    {
      WebPartChrome theChrome = new MyWebPartChrome(this, 
        this.WebPartManager);
      if (RenderVerbsInMenu)
        this.WebPartVerbRenderMode = WebPartVerbRenderMode.Menu;
      else
        this.WebPartVerbRenderMode = WebPartVerbRenderMode.TitleBar;
      return theChrome;
    }

    public Boolean RenderVerbsInMenu
    {
      get { return _renderVerbsInMenu; }
      set { _renderVerbsInMenu = value; }
    }
  }

  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  public class MyWebPartChrome : WebPartChrome
  {
    WebPartZoneBase theZone;
    WebPartManager theManager;

    public MyWebPartChrome(WebPartZoneBase aZone, WebPartManager aManager) : 
      base(aZone, aManager)
    {
      theZone = aZone;
      theManager = aManager;
    }

    protected override WebPartVerbCollection GetWebPartVerbs(WebPart webPart)
    {
      ArrayList verbSet = new ArrayList();
      foreach (WebPartVerb verb in base.GetWebPartVerbs(webPart))
      {
        if (verb.Text != "Close")
          verbSet.Add(verb);
      }
      WebPartVerbCollection reducedVerbSet = 
        new WebPartVerbCollection(verbSet);
      return reducedVerbSet;
    }

    protected override Style CreateWebPartChromeStyle(WebPart part, 
      PartChromeType chromeType)
    {
      Style finalStyle = new Style();
      finalStyle.CopyFrom(base.CreateWebPartChromeStyle(part, chromeType));
      finalStyle.Font.Name = "Verdana";
      return finalStyle;
    }

    protected override void RenderPartContents(HtmlTextWriter writer, 
      WebPart part)
    {

        if (part == this.WebPartManager.SelectedWebPart)
          HttpContext.Current.Response.Write("<span>Not rendered</span>");
        else
          if(this.Zone.GetType() == typeof(MyZone))
            part.RenderControl(writer);
    }
  }
}
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts

Namespace Samples.AspNet.VB.Controls

  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Class MyZone
    Inherits WebPartZone
    Private _renderVerbsInMenu As Boolean

    Protected Overrides Function CreateWebPartChrome() As WebPartChrome
      Dim theChrome As WebPartChrome = _
        New MyWebPartChrome(Me, Me.WebPartManager)
      If RenderVerbsInMenu Then
        Me.WebPartVerbRenderMode = WebPartVerbRenderMode.Menu
      Else
        Me.WebPartVerbRenderMode = WebPartVerbRenderMode.TitleBar
      End If
      Return theChrome
    End Function

    Public Property RenderVerbsInMenu() As Boolean
      Get
        Return _renderVerbsInMenu
      End Get
      Set(ByVal value As Boolean)
        _renderVerbsInMenu = value
      End Set
    End Property

  End Class

  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Class MyWebPartChrome
    Inherits WebPartChrome
    Dim theManager As WebPartManager
    Dim theZone As WebPartZoneBase

    Public Sub New(ByVal aZone As WebPartZoneBase, _
      ByVal aManager As WebPartManager)

      MyBase.New(aZone, aManager)
      theManager = aManager
      theZone = aZone
    End Sub

    Protected Overrides Function GetWebPartVerbs _
      (ByVal webPart As WebPart) As WebPartVerbCollection

      Dim verbSet As New ArrayList()
      Dim verb As WebPartVerb
      For Each verb In MyBase.GetWebPartVerbs(webPart)
        If verb.Text <> "Close" Then
          verbSet.Add(verb)
        End If
      Next verb

      Dim reducedVerbSet As WebPartVerbCollection = _
        New WebPartVerbCollection(verbSet)

      Return reducedVerbSet
    End Function

    Protected Overrides Function CreateWebPartChromeStyle _
      (ByVal part As WebPart, ByVal chromeType As PartChromeType) As Style

      Dim finalStyle As New Style()
      finalStyle.CopyFrom(MyBase.CreateWebPartChromeStyle(Part, chromeType))
      finalStyle.Font.Name = "Verdana"
      Return finalStyle
    End Function

    Protected Overrides Sub RenderPartContents _
      (ByVal writer As HtmlTextWriter, ByVal part As WebPart)

      If part Is Me.WebPartManager.SelectedWebPart Then
        HttpContext.Current.Response.Write("<span>Not rendered</span>")
      Else
        If (Me.Zone.GetType() Is GetType(MyZone)) Then
          part.RenderControl(writer)
        End If
      End If

    End Sub

  End Class

End Namespace

当您在浏览器中加载网页时,您可以看到在 MyWebPartChrome 类的源代码中所做的各种样式和其他呈现自定义项如何出现在该区域中呈现的 WebPart 控件上。When you load the Web page in a browser, you can see how the various stylistic and other rendering customizations made in the source code of the MyWebPartChrome class appear on the WebPart controls rendered in the zone.

注解

Web 部件 chrome 是指外围用户界面(UI)元素,这些元素构成了 WebPartZoneBase 区域中包含的每个 WebPart 控件或服务器控件。Web Parts chrome refers to the peripheral user interface (UI) elements that frame each WebPart control or server control contained in a WebPartZoneBase zone. 控件的 chrome 包含其边框、标题栏和标题栏中显示的图标、标题文本和谓词菜单。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. Chrome 的外观是在区域级别设置的,并应用于区域中的所有服务器控件。The appearance of the chrome is set at the zone level, and applies to all the server controls in the zone.

Web 部件控件集使用 WebPartChrome 类呈现 WebPart 控件的 chrome。The Web Parts control set uses the WebPartChrome class to render the chrome for WebPart controls. 此外,此类提供了一种方法,使开发人员可以自定义 WebPartZoneBase 区域中的 WebPart 控件的任何单个部分(如页眉或页脚),而无需处理这些控件的所有呈现。Additionally, this class provides a way for developers to customize the rendering of any individual section (such as the header or footer) of the WebPart controls in a WebPartZoneBase zone without having to handle all the rendering for those controls. 例如,你可以重写 CreateWebPartChromeStyle 方法,以自定义应用于 WebPartZoneBase 区域的某些特定样式属性,但你可以依赖于默认呈现来处理 rest。For example, you can override the CreateWebPartChromeStyle method to customize some specific style attributes applied to the WebPartZoneBase zone, but you can rely on the default rendering to handle the rest.

WebPartChrome 类包含几个重要的方法,当您想要重写 WebPart 控件的呈现时,这些方法非常有用。The WebPartChrome class contains several important methods that are useful when you want to override the rendering of WebPart controls. 其中一种是 CreateWebPartChromeStyle 方法,使您可以更改与用于呈现控件的 WebPartChrome 对象关联的 Style 对象的样式特性。One is the CreateWebPartChromeStyle method, which enables you to change the style attributes of the Style object associated with the WebPartChrome object used to render a control. 另一个是 WebPartChrome 构造函数,当你在自定义 WebPartZoneBase 类中重写 CreateWebPartChrome 方法以创建自定义 WebPartChrome 对象的实例时,将使用该构造函数。Another is the WebPartChrome constructor, which you use when you override the CreateWebPartChrome method in a custom WebPartZoneBase class to create an instance of your custom WebPartChrome object. 另一种有用的方法是 RenderPartContents 方法,您可以使用该方法控制区域中控件的内容区域(而不是诸如页眉、页脚和标题栏等 chrome 元素)的呈现。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). 如果出于某种原因而不希望用户在用户界面中查看这些谓词,则 GetWebPartVerbs 方法使您能够排除呈现某些谓词。The GetWebPartVerbs method enables you to exclude certain verbs from being rendered, if for some reason you do not want users to see those verbs in the UI. 最后,如果你想要完全以编程方式控制呈现 WebPart 控件的所有方面,则可以重写 RenderWebPart 方法。Finally, if you want complete programmatic control over all aspects of rendering the WebPart controls, you can override the RenderWebPart method.

WebPartChrome 类的多个属性也很有用。Several properties of the WebPartChrome class are also useful. ZoneWebPartManager 属性分别提供对与 WebPartChrome 对象和当前 WebPartManager 实例相关联的区域的引用。The Zone and WebPartManager properties provide references to the zone associated with the WebPartChrome object and the current WebPartManager instance, respectively.

继承者说明

如果要重写 WebPart 或服务器控件的某些节的默认呈现,请从 WebPartChrome 类继承。Inherit from the WebPartChrome class when you want to override the default rendering of certain sections of WebPart or server controls. 您可以选择重写 CreateWebPartChromeStyle(WebPart, PartChromeType) 方法、GetWebPartVerbs(WebPart) 方法,特别是 RenderPartContents(HtmlTextWriter, WebPart)RenderWebPart(HtmlTextWriter, WebPart) 方法,以自定义呈现。You can optionally override the CreateWebPartChromeStyle(WebPart, PartChromeType) method, the GetWebPartVerbs(WebPart) method, and especially the RenderPartContents(HtmlTextWriter, WebPart) or RenderWebPart(HtmlTextWriter, WebPart) methods, to customize the rendering. 若要将自定义 WebPartChrome 类用于区域中的 WebPart 控件,请开发从 WebPartZoneBase 类派生的自定义区域,并重写其 CreateWebPartChrome() 方法。To use your custom WebPartChrome class for the WebPart controls in a zone, develop a custom zone derived from the WebPartZoneBase class, and override its CreateWebPartChrome() method. 有关如何在区域中使用子 WebPartChrome 对象的详细信息,请参阅 "示例" 部分。For details on how to use a child WebPartChrome object in a zone, see the Example section.

构造函数

WebPartChrome(WebPartZoneBase, WebPartManager)

初始化控件的新实例。Initializes a new instance of the control.

属性

DragDropEnabled

获取指示是否可以将控件拖入和拖出区域的值。Gets a value that indicates whether controls can be dragged into and out of the zone.

WebPartManager

获取对当前 WebPartManager 实例的引用。Gets a reference to the current WebPartManager instance.

Zone

获取对关联的 WebPartZoneBase 区域的引用。Gets a reference to the associated WebPartZoneBase zone.

方法

CreateWebPartChromeStyle(WebPart, PartChromeType)

创建样式对象,该对象为 WebPart 对象呈现的每个 WebPartChrome 控件提供样式特性。Creates the style object that supplies style attributes for each WebPart control rendered by the WebPartChrome object.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
FilterWebPartVerbs(WebPartVerbCollection, WebPart)

基于开发人员提供的条件,从呈现中排除特定谓词。Excludes specific verbs from being rendered, based on criteria provided by a developer.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
GetWebPartChromeClientID(WebPart)

获取网页中呈现的 WebPartChrome 对象的客户端 ID。Gets the client ID for the WebPartChrome object as rendered in a Web page.

GetWebPartTitleClientID(WebPart)

获取表单元格的客户端 ID,该表单元格包含 WebPart 控件的标题。Gets the client ID for the table cell that contains the title for a WebPart control.

GetWebPartVerbs(WebPart)

获取应使用 WebPart 控件呈现的谓词的集合。Gets a collection of verbs that should be rendered with a WebPart control.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
PerformPreRender()

执行在呈现 WebPart 控件之前必须完成的任务。Performs tasks that must be done prior to rendering WebPart controls.

RenderPartContents(HtmlTextWriter, WebPart)

呈现 WebPart 控件的主要内容区域,不包括页眉和页脚。Renders the main content area of a WebPart control, excluding the header and footer.

RenderWebPart(HtmlTextWriter, WebPart)

呈现完整的 WebPart 控件及其所有部分。Renders a complete WebPart control with all its sections.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅