Export (0) Print
Expand All

CatalogPart Class

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

Serves as the base class for controls that reside in CatalogZoneBase zones, and that provide catalogs of available Web server controls (especially WebPart controls) that users can add to a Web page.

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

[BindableAttribute(false)] 
public abstract class CatalogPart : Part
/** @attribute BindableAttribute(false) */ 
public abstract class CatalogPart extends Part
BindableAttribute(false) 
public abstract class CatalogPart extends Part

The CatalogPart class is a base class for special controls that reside in CatalogZoneBase zones. These derived CatalogPart controls provide catalogs of Web server controls that end users can add to a Web page. Use CatalogPart controls in a Web application when you want to provide end users with the flexibility to change the functionality of a Web page by adding or removing server controls.

A catalog is simply a list of one or more available Web server controls (including WebPart controls, ASP.NET server controls, and custom or user controls) that users can add to a Web page. A catalog has a number of common characteristics, including instruction text for end users; text to describe each server control; helper controls to select the server controls and add them to the page; a common header, footer, and border; and a number of style attributes.

There are three types of CatalogPart controls provided with the Web Parts control set, as described in the following table. Each type of CatalogPart control contains server controls that are added to a page from a different source.

NoteImportant:

Controls that inherit from the CatalogPart class can reside only in zones derived from the CatalogZoneBase class.

Control

Description

PageCatalogPart

Contains controls that have been closed on a page, and that can be reopened (added back to the page) by users. The controls in this type of catalog are controls that were already added to the page from another source, and were then closed by a user.

DeclarativeCatalogPart

Contains server controls declared within a DeclarativeCatalogPart control, which is itself contained in a CatalogZoneBase zone in the markup of a Web page.

ImportCatalogPart

Provides the user interface (UI) for a user to upload a control's definition file (an XML file defined by a schema, that contains state information) to a catalog, so that the control can be added to a Web page. The controls themselves do not appear in this type of catalog; the catalog is just a mechanism for accessing the definition files for external controls so they can be added to a page.

The CatalogPart class inherits from the base Part class, so that it has the common behavior of other part controls in the Web Parts control set. In addition, it adds some members that are useful for the catalog functionality. The DisplayTitle property gets the actual string that is currently being displayed as the title of the control. The value of this property can be the same as the value of the Title property or, if no value has been assigned to that property, a default value supplied by the .NET Framework. The WebPartManager and Zone properties both provide convenient access to the two essential objects that control the life cycle of a CatalogPart control: the WebPartManager control, and the CatalogZoneBase zone that contains the CatalogPart control, respectively.

The CatalogPart class also contains several methods. The GetAvailableWebPartDescriptions method is declared as an abstract method that must be implemented by inheritors. Its intended purpose is to return a collection of descriptions of the controls in the catalog. A related method, GetWebPart, is also declared as abstract and must be implemented by inheritors. This method is intended to return an instance of a WebPart control based on a description that is passed to the method as a parameter.

Notes to Inheritors Because the CatalogPart class is abstract, you cannot use it directly on a Web page. The Web Parts control set provides three CatalogPart controls (listed in the table in the Remarks section of this topic) that derive from the base class and that can be used on Web Parts pages. These controls should provide most of the features needed to create catalogs of Web server controls. However, you might have specialized needs that would require developing a custom CatalogPart control. For example, you can create a special type of CatalogPart control that makes server controls available through a Web service, or directly from a database. To do this, you must inherit from the CatalogPart class. You must also override the abstract GetAvailableWebPartDescriptions and GetWebPart methods to provide an implementation that returns the WebPart or other server controls, and their descriptions. You will also need methods to load the various server controls from your database or Web service.

TopicLocation
How to: Set the Display Mode of a Web Parts PageBuilding ASP .NET Web Applications
How to: Set the Display Mode of a Web Parts PageBuilding ASP .NET Web Applications

The following code example demonstrates the use of the CatalogPart class. Because the class is abstract, it is not used directly in code. However, the example shows how to work with the three derived CatalogPart controls provided with the Web Parts control set to demonstrate common usage scenarios.

There are four parts to the code example:

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

  • A Web page, which contains the three CatalogPart controls provided with the Web Parts control set, all declared within a CatalogZone on a Web page. The page also contains a WebPartZone control, with an AdRotator control declared in the zone.

  • An XML file that contains quotations displayed in the AdRotator control on the Web page.

  • An explanation of how to run the example.

The first part of this code example is the user control that enables users to change display modes on a Web page. You should save this code in a file named Displaymodemenuscs.ascx or Displaymodemenuvb.ascx (depending on which language you are using). For details about display modes and a description of the source 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 the Web page. The page contains a CatalogZone control, and within the child <zonetemplate> element, each of the three CatalogPart controls provided with the Web Parts control set is declared. Some attribute are set on the DeclarativeCatalogPart control, and also in the <script> section of the page, the attributes on that control are accessed programmatically. The control contains one child control that a user can add to the page: a standard Calendar control. The CatalogZone and CatalogPart control will only be visible when the user switches the page to catalog display mode.

The page also contains a WebPartZone control, and within its child <zonetemplate> element is an AdRotator control that displays alternating messages from an XML file. If a user closes the control by clicking its close verb when the page is displayed, the control is added to the page catalog, and the user can reopen the control by switching the page into catalog display mode, accessing the PageCatalogPart control, and adding back the closed control.

<%@ Page Language="C#" %>
<%@ 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">

<script runat="server">
  
  protected void Button1_Click(object sender, EventArgs e)
  {
    
    Label1.Text = "<h3>DeclarativeCatalogPart Property Values</h3>" +
      "Display Title: " + DeclarativeCatalogPart1.DisplayTitle + 
      "<br />" + 
      "Description: " + DeclarativeCatalogPart1.Description + 
      "<br />" + 
      "Chrome type: " + DeclarativeCatalogPart1.ChromeType.ToString();
  }

  protected void WebPartManager1_DisplayModeChanged(object sender, 
    WebPartDisplayModeEventArgs e)
  {
    Label1.Text = String.Empty;
    if (WebPartManager1.DisplayMode == WebPartManager.CatalogDisplayMode)
      Button1.Visible = true;
    else
      Button1.Visible = false;
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CatalogPart Samples</title>
</head>
<body>
  <form id="form1" runat="server">
    <asp:WebPartManager ID="WebPartManager1" runat="server" 
      OnDisplayModeChanged="WebPartManager1_DisplayModeChanged" />
    <uc1:DisplayModeMenuCS ID="DisplayModeMenu1" runat="server" />
    <asp:WebPartZone ID="WebPartZone1" runat="server">
      <ZoneTemplate>
        <asp:AdRotator ID="AdRotator1" runat="server" 
          AdType="Banner" 
          AdvertisementFile="~/quotes.xml" 
          Title="Favorite Quotes"  />         
      </ZoneTemplate>
    </asp:WebPartZone>
    <asp:CatalogZone ID="CatalogZone1" runat="server">
      <ZoneTemplate>
        <asp:DeclarativeCatalogPart 
          ID="DeclarativeCatalogPart1" 
          runat="server"
          Title="Controls to Add"
          ChromeType="TitleOnly"
          Description="Provides a list of controls that users can
            add to the page.">
          <WebPartsTemplate>
            <asp:Calendar ID="Calendar1" runat="server" 
              Title="My Calendar" />         
          </WebPartsTemplate>
        </asp:DeclarativeCatalogPart>
        <asp:PageCatalogPart ID="PageCatalogPart1" runat="server" />
        <asp:importcatalogpart id="ImportCatalogPart1" runat="server" />
      </ZoneTemplate>
    </asp:CatalogZone>
    <hr />
    <asp:Button ID="Button1" runat="server" 
      Text="Display DeclarativeCatalogPart Properties" 
      OnClick="Button1_Click" 
      Visible="false"/>
    <br />
    <asp:Label ID="Label1" runat="server" Text="" />
  </form>
</body>
</html>

The fourth part of the code example is the XML file. This file is a source file for the AdRotator control that is declared on the page. The file contains favorite quotations, which the control periodically rotates and displays. Copy the following content into a text editor, and save the file as Quotes.xml.

<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
  <Ad>
    <AlternateText>
      A stitch in time saves nine.
    </AlternateText>    
  </Ad>
  <Ad>
    <AlternateText>
      A penny saved is a penny earned.
    </AlternateText>    
  </Ad>
</Advertisements>

To run the code example, load the Web page in a browser. Use the Display Modes drop-down control to select Catalog and switch the page to catalog display mode. Notice the effects of the various property values that were set on the DeclarativeCatalogPart control that contains the Calendar control. You can select the check box next to the Calendar control, and click Add to add it to the page. If you click the Display DeclarativeCatalogPart Properties button, the values of the properties for that control are displayed. You can click Close to return the page to normal browse mode and see the added control on the page. Now go to the verbs menu on the AdRotator control (represented by the downward arrow in the title bar) and click Close. The control is closed and added to the page catalog. Switch the page into catalog display mode again, and click the Page Catalog hyperlink to display the PageCatalogPart control. Note that the AdRotator control is referenced there by its title, Favorite Quotes. Select the Favorite Quotes control, and click the Add button to add the control back to the page. Click the Close button to return the page to browse mode.

NoteNote

The ImportCatalogPart control is also declared on this page, but a sample showing how to import controls is more involved, so full use of the control is not demonstrated in this code example. To see a full working example, see the class overview documents for the ImportCatalogPart class or the CatalogZone 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