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:
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 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. 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 zone using the element <aspSample:MyZone> and includes several standard ASP.NET server controls within the zone. 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. 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="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 >
<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>
<%@ 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 >
<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>
The third part of the code example is the source for the custom WebPartZoneBase zone and a WebPartChrome class. In the constructor for the custom zone class, the code checks the MyZone.RenderVerbsInMenu property. If the value is true, verbs are rendered in a menu for each of the WebPart controls in the zone. This is the normal, default behavior in the Web Parts controls set. 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. 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. To experiment with this feature, you can set the declarative RenderVerbsInMenu attribute to false, and note how the verbs are rendered as links.
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. You can compile it explicitly and put the resulting assembly in your Web site's Bin folder or the global assembly cache. Alternatively, you can put the source code in your site's App_Code folder, where it will be dynamically compiled at run time. For a walkthrough that demonstrates both methods of compiling, see Walkthrough: Developing and Using a Custom Server Control.
Imports System
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
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);
}
}
}
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.