Export (0) Print
Expand All

WebPartZoneBase Class

Note: This class is new in the .NET Framework version 2.0.

Serves as the base class for all zone controls that act as containers for WebPart (or other server or user) controls.

Namespace: System.Web.UI.WebControls.WebParts
Assembly: System.Web (in system.web.dll)

public abstract class WebPartZoneBase : WebZone, IPostBackEventHandler
public abstract class WebPartZoneBase extends WebZone implements IPostBackEventHandler
public abstract class WebPartZoneBase extends WebZone implements IPostBackEventHandler

In the Web Parts control set, a zone is a composite control, rendered as an HTML table, that contains other controls in a defined region of a Web page. The WebPartZoneBase class is a zone that inherits from the base WebZone class and provides a base set of behaviors for derived zones that contain WebPart controls, server controls, and user controls. The combination of WebPartZoneBase zones with WebPart (and other server) controls forms the primary UI of a Web Parts application, the normal view of a page that users interact with most of the time.

All zones provide a common set of UI elements for the controls they contain. For a detailed discussion of the UI elements common to all zones, see the class overview topic for the WebZone class. The WebPartZoneBase zone includes a body section that contains server controls, and an associated WebPartChrome object used for rendering the contained controls and their chrome elements, such as their verbs, borders, and so on.

In addition to the features inherited from the WebZone class, the WebPartZoneBase class adds client-side control dragging, specific verbs (to enable common UI actions such as minimizing, closing, deleting, and editing controls), and additional style features. The WebPartZoneBase class is also the class to inherit from if you want to design a custom zone that is database-driven. For example, you could create a custom weather Web Parts zone that inherits from WebPartZoneBase, and is designed to contain and provide layout features and other services for WebPart controls that are data-bound and display weather information.

The WebPartZoneBase class contains a number of properties for working with style attributes in a zone. There are several properties for borders, including BorderStyle and BorderWidth. There is a group of properties for working with style attributes of menus that can appear in the header of a zone, such as MenuLabelStyle, MenuVerbStyle, and others.

The WebPartZoneBase class also contains a number of members for handling verbs in a zone. These members relate to the verbs that appear on the part controls in the zone. While zone-level verbs can be added to WebPartZoneBase zones, by default they do not have any. There is a set of standard verbs provided with the Web Parts control set for use with part controls, and developers can add custom verbs as well. Some important properties for working with verbs include properties that reference some of the standard verb objects, such as CloseVerb, ConnectVerb, DeleteVerb, and EditVerb, HelpVerb, and MinimizeVerb. The standard verbs appear on a verbs menu (typically presented in the UI as a drop-down menu) in the title bar of each control contained in a zone. There is also a VerbButtonType property that allows you to determine what type of clickable object represents a verb in the UI.

Other key members for working with verbs include the OnCreateVerbs method, which is an event handler that can be overridden for custom handling of the verb creation process, and the CreateVerbs event.

The WebPartZoneBase class contains a number of members for working with WebPart controls contained within a zone. The WebParts property references the collection of all WebPart (and other server) controls in the zone. Several methods correspond to the standard verbs or actions that a user can carry out on WebPart controls in a zone, such as CloseWebPart, ConnectWebPart, and EditWebPart.

There are also a number of members in the WebPartZoneBase class that concern how the WebPart controls are laid out or arranged within the zone. The AllowLayoutChange property determines whether controls can be moved among zones or rearranged within a zone by users. The LayoutOrientation property allows you to determine whether the controls in a zone are arranged horizontally or vertically.

Other methods in the WebPartZoneBase class provide you with detailed programmatic control over the rendering of the various areas of a zone. Many of these methods override the base methods inherited from the WebZone class, to customize the rendering for zones that contain WebPart controls. Important methods include Render, RenderBody, RenderDropCue, and RenderHeader.

The following code example demonstrates the use of the WebPartZoneBase class. The code shows how a derived class, WebPartZone, is used declaratively in a Web Parts page to contain WebPart controls. The code example has four sections: the first three sections describe the code files, and the fourth explains how to run the code.

The first part of the code example is a user control that enables users to switch the page into different display modes. For more information on Web Parts display modes and a description of the code in this control, see Walkthrough: Changing Display Modes on a Web Parts Page.

<%@ control language="C#" classname="DisplayModeMenuCS"%>
<script runat="server">
  
 // Use a field to reference the current WebPartManager.
  WebPartManager _manager;

  void Page_Init(object sender, EventArgs e)
  {
    Page.InitComplete += new EventHandler(InitComplete);
  }  

  void InitComplete(object sender, System.EventArgs e)
  {
    _manager = WebPartManager.GetCurrentWebPartManager(Page);

    String browseModeName = WebPartManager.BrowseDisplayMode.Name;

    // Fill the dropdown with the names of supported display modes.
    foreach (WebPartDisplayMode mode in _manager.SupportedDisplayModes)
    {
      String modeName = mode.Name;
      // Make sure a mode is enabled before adding it.
      if (mode.IsEnabled(_manager))
      {
        ListItem item = new ListItem(modeName, modeName);
        DisplayModeDropdown.Items.Add(item);
      }
    }

    // If shared scope is allowed for this user, display the scope-switching
    // UI and select the appropriate radio button for the current user scope.
    if (_manager.Personalization.CanEnterSharedScope)
    {
      Panel2.Visible = true;
      if (_manager.Personalization.Scope == PersonalizationScope.User)
        RadioButton1.Checked = true;
      else
        RadioButton2.Checked = true;
    }
    
  }
 
  // Change the page to the selected display mode.
  void DisplayModeDropdown_SelectedIndexChanged(object sender, EventArgs e)
  {
    String selectedMode = DisplayModeDropdown.SelectedValue;

    WebPartDisplayMode mode = _manager.SupportedDisplayModes[selectedMode];
    if (mode != null)
      _manager.DisplayMode = mode;
  }

  // Set the selected item equal to the current display mode.
  void Page_PreRender(object sender, EventArgs e)
  {
    ListItemCollection items = DisplayModeDropdown.Items;
    int selectedIndex = 
      items.IndexOf(items.FindByText(_manager.DisplayMode.Name));
    DisplayModeDropdown.SelectedIndex = selectedIndex;
  }

  // Reset all of a user's personalization data for the page.
  protected void LinkButton1_Click(object sender, EventArgs e)
  {
    _manager.Personalization.ResetPersonalizationState();
  }

  // If not in User personalization scope, toggle into it.
  protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
  {
    if (_manager.Personalization.Scope == PersonalizationScope.Shared)
      _manager.Personalization.ToggleScope();
  }

  // If not in Shared scope, and if user is allowed, toggle the scope.
  protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
  {
    if (_manager.Personalization.CanEnterSharedScope && 
        _manager.Personalization.Scope == PersonalizationScope.User)
      _manager.Personalization.ToggleScope();
  }
</script>
<div>
  <asp:Panel ID="Panel1" runat="server" 
    Borderwidth="1" 
    Width="230" 
    BackColor="lightgray"
    Font-Names="Verdana, Arial, Sans Serif" >
    <asp:Label ID="Label1" runat="server" 
      Text="&nbsp;Display Mode" 
      Font-Bold="true"
      Font-Size="8"
      Width="120" />
    <asp:DropDownList ID="DisplayModeDropdown" runat="server"  
      AutoPostBack="true" 
      Width="120"
      OnSelectedIndexChanged="DisplayModeDropdown_SelectedIndexChanged" />
    <asp:LinkButton ID="LinkButton1" runat="server"
      Text="Reset User State" 
      ToolTip="Reset the current user's personalization data for the page."
      Font-Size="8" 
      OnClick="LinkButton1_Click" />
    <asp:Panel ID="Panel2" runat="server" 
      GroupingText="Personalization Scope"
      Font-Bold="true"
      Font-Size="8" 
      Visible="false" >
      <asp:RadioButton ID="RadioButton1" runat="server" 
        Text="User" 
        AutoPostBack="true"
        GroupName="Scope" OnCheckedChanged="RadioButton1_CheckedChanged" />
      <asp:RadioButton ID="RadioButton2" runat="server" 
        Text="Shared" 
        AutoPostBack="true"
        GroupName="Scope" 
        OnCheckedChanged="RadioButton2_CheckedChanged" />
    </asp:Panel>
  </asp:Panel>
</div>

The second part of the code example is a code-beside file that contains the code to handle events for the main Web page. The code in this file shows how to work with some of the key WebPartZoneBase members programmatically.

using System;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class WebPartZoneBase_overview : System.Web.UI.Page
{
  protected void Page_Load(Object sender, EventArgs e)
  {
    Label1.Text = DateTime.Now.ToLongDateString();
    Label2.Text = String.Empty;
  }

  protected void Button1_Click(object sender, EventArgs e)
  {
    if (WebPartZone2.VerbButtonType == ButtonType.Button)
      WebPartZone2.VerbButtonType = ButtonType.Link;
    else
      WebPartZone2.VerbButtonType = ButtonType.Button;
  }

  protected void Button2_Click(object sender, EventArgs e)
  {
    if (WebPartZone1.LayoutOrientation == Orientation.Vertical)
      WebPartZone1.LayoutOrientation = Orientation.Horizontal;
    else
      WebPartZone1.LayoutOrientation = Orientation.Vertical;
    Page_Load(sender, e);
  }

  protected void Button3_Click(object sender, EventArgs e)
  {
    StringBuilder builder = new StringBuilder();
    builder.AppendLine(@"<strong>WebPartZone1 WebPart IDs</strong><br />");
    foreach (WebPart part in WebPartZone1.WebParts)
    {
      builder.AppendLine("ID: " + part.ID 
                          + "; Type:  " + part.GetType() 
                          + @"<br />");
    }
    Label2.Text = builder.ToString();
    Label2.Visible = true;
  }

  protected void Button4_Click(object sender, EventArgs e)
  {
    StringBuilder builder = new StringBuilder();
    builder.AppendLine(@"<strong>WebPartZone1 DisplayTitle Property</strong><br />");
    builder.AppendLine(WebPartZone1.DisplayTitle + @"<br />");
    Label2.Text = builder.ToString();
    Label2.Visible = true;
  }
}

The third part of the code example is the Web page that contains the WebPartZone controls that exhibit behavior from the base WebPartZoneBase class, and WebPart controls contained in the zones.

<%@ Page Language="C#" 
  Codefile="webpartzonebase_overview.cs"
  Inherits="WebPartZoneBase_overview"  %>
<%@ register TagPrefix="uc1" 
  TagName="DisplayModeMenuCS" 
  Src="DisplayModeMenuCS.ascx" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>WebPartZoneBase Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="WebPartManager1" Runat="server" />
      <uc1:DisplayModeMenuCS ID="DisplayModeMenu1" runat="server" />
      <table width="75%">
        <tr>
          <td>
            <asp:WebPartZone 
              ID="WebPartZone1" 
              Runat="server"
              LayoutOrientation="Vertical" >
              <EditVerb Text="Edit WebPart" />
              <SelectedPartChromeStyle BackColor="LightBlue" />
              <ZoneTemplate>
                <asp:BulletedList 
                  ID="BulletedList1" 
                  Runat="server"
                  DisplayMode="HyperLink" 
                  Title="Favorite Links" >
                  <asp:ListItem Value="http://msdn.microsoft.com">
                    MSDN
                  </asp:ListItem>
                  <asp:ListItem Value="http://www.asp.net">
                    ASP.NET
                  </asp:ListItem>
                  <asp:ListItem Value="http://www.msn.com">
                    MSN
                  </asp:ListItem>
                </asp:BulletedList>
                <asp:Calendar ID="Calendar1" Runat="server" 
                  Title="My Calendar" />
              </ZoneTemplate>
            </asp:WebPartZone>
          </td>
          <td valign="top" align="right">
            <asp:WebPartZone 
              ID="WebPartZone2"
              Runat="server" 
              DragHighlightColor="#00ff00"
              AllowLayoutChange="true"
              EmptyZoneText="Add WebParts to this empty Zone."
              BorderWidth="2"
              BorderColor="DarkBlue"
              BorderStyle="Dashed" 
              MenuLabelText="Verbs Menu" 
              MenuPopupImageUrl="label.gif" >
              <VerbStyle Font-Italic="true" />
              <MenuLabelStyle BackColor="Lime" BorderWidth="1"  />
              <MenuLabelHoverStyle Font-Bold="true" />
              <MenuVerbHoverStyle BackColor="LightGrey" />
              <MenuVerbStyle Font-Italic="true" /> 
              <ZoneTemplate>
                <asp:Label ID="Label1" Runat="server" Title="Date" />
              </ZoneTemplate>
            </asp:WebPartZone>
          </td>
        </tr>
        <tr>
          <td>
            <asp:EditorZone ID="EditorZone1" Runat="server">
              <ZoneTemplate>
                <asp:AppearanceEditorPart ID="AppearanceEditorPart1" 
                  Runat="server" />
                <asp:LayoutEditorPart ID="LayoutEditorPart1" 
                  Runat="server" />
              </ZoneTemplate>
            </asp:EditorZone>      
          </td>
        </tr>
      </table>
    </div>
    <asp:Button ID="Button1" Runat="server" 
      Width="200" Text="Toggle WebPartZone2 Buttons" 
      OnClick="Button1_Click" />
    <br />
    <asp:Button ID="Button2" Runat="server" 
      Width="200"  Text="Toggle Layout Orientation" 
      OnClick="Button2_Click" />
    <br />
    <asp:Button ID="Button3" Runat="server" 
      Width="200" Text="List WebPartZone1 WebParts" 
      OnClick="Button3_Click" />
    <br />
    <asp:Button ID="Button4" Runat="server" 
      Width="200" Text="WebPartZone1 Display Title" 
      OnClick="Button4_Click" />
    <br />
    <asp:Label ID="Label2" Runat="server" Visible="false" />
    </form>
</body>
</html>

Load the Web page in a browser. Use the Display Mode drop-down list control to switch the page into design mode, click the WebPart controls, and try dragging them between the visible zones. Click some of the buttons on the page to see the effect of using various members of the WebPartZoneBase class. Next, switch the page to edit mode. Make sure a WebPart control is positioned in WebPartZone2, click the verbs menu on the control's title bar, and click Edit to edit the control. Finish editing that control, and then edit one of the controls in the other zone. As you carry out these steps, notice the differences in the UI styles of the controls and the zones, which are the effect of using the various members of the WebPartZoneBase class.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft