CatalogPartCollection Class


Contains a collection of CatalogPart controls used to provide catalogs of Web server controls that end users can add to a Web page. This class cannot be inherited.

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


public sealed class CatalogPartCollection : ReadOnlyCollectionBase


Initializes a new, empty instance of the CatalogPartCollection class.

System_CAPS_pubmethodCatalogPartCollection(CatalogPartCollection, ICollection)

Initializes a new instance of the CatalogPartCollection class by passing in an ICollection collection of the existing CatalogPart controls in a zone, and an additional collection of controls.


Initializes a new instance of the CatalogPartCollection class by passing in an ICollection collection of CatalogPart controls.


Gets the number of elements contained in the ReadOnlyCollectionBase instance.(Inherited from ReadOnlyCollectionBase.)


Gets or sets a member of the collection, based on its position in the collection.


Returns a member of the collection based on a unique string identifier.


Returns a value indicating whether a particular control exists in the collection.

System_CAPS_pubmethodCopyTo(CatalogPart[], Int32)

Copies the collection to an array of CatalogPart objects.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Returns an enumerator that iterates through the ReadOnlyCollectionBase instance.(Inherited from ReadOnlyCollectionBase.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Returns the position of a particular member of the collection.


Returns a string that represents the current object.(Inherited from Object.)


References a static, read-only, empty instance of the collection.

System_CAPS_pubinterfaceSystem_CAPS_privmethodICollection.CopyTo(Array, Int32)

Copies the entire ReadOnlyCollectionBase to a compatible one-dimensional Array, starting at the specified index of the target array.(Inherited from ReadOnlyCollectionBase.)


Gets a value indicating whether access to a ReadOnlyCollectionBase object is synchronized (thread safe).(Inherited from ReadOnlyCollectionBase.)


Gets an object that can be used to synchronize access to a ReadOnlyCollectionBase object.(Inherited from ReadOnlyCollectionBase.)


Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)


Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)


Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)


Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)

The CatalogPartCollection class is a read-only collection of CatalogPart controls, typically used by a CatalogZoneBase zone to track the set of CatalogPart controls contained by the zone.

When a Web Parts page enters catalog mode, the zone creates a new CatalogPartCollection object consisting of the CatalogPart controls. Each CatalogPart control in the collection can contain references to zero or more Web server controls, which are displayed in the form of a catalog of available server controls.

You can create a CatalogPartCollection collection of controls for your own programmatic use if, for example, you need to perform some mass operation on a set of CatalogPart controls. Even though the CatalogPartCollection object is read-only, you can make programmatic changes to the underlying controls referenced in the collection.

The following code example demonstrates several uses of the CatalogPartCollection class. There are four parts to this code example:

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

  • A class for a custom WebPart control named TextDisplayWebPart, which is referenced in the Web page and is contained in one of the CatalogPart controls.

  • A Web page that references the TextDisplayWebPart control, contains a CatalogZone control with two of the CatalogPart controls from the Web Parts control set declared in the zone, and contains event-driven code to create and manipulate a CatalogPartCollection object.

  • An explanation of how the code example works when you load it in a browser.

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 code example is the TextDisplayWebPart control. 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 Web Server Control. Note that the control has a property named ContentText; this property holds the value that the user enters in the text box.

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
    Level = AspNetHostingPermissionLevel.Minimal)]
    Level = AspNetHostingPermissionLevel.Minimal)]
  public class TextDisplayWebPart : WebPart
    private String _contentText = null;
    TextBox input;
    Label DisplayContent;
    Literal lineBreak;

    [Personalizable(), WebBrowsable]
    public String ContentText
      get { return _contentText; }
      set { _contentText = value; }

    protected override void CreateChildControls()
      DisplayContent = new Label();
      DisplayContent.BackColor = Color.LightBlue;
      DisplayContent.Text = this.ContentText;

      lineBreak = new Literal();
      lineBreak.Text = @"<br />";

      input = new TextBox();
      Button update = new Button();
      update.Text = "Set Label Content";
      update.Click += new EventHandler(this.submit_Click);


    private void submit_Click(object sender, EventArgs e)
      // Update the label string.
      if (input.Text != String.Empty)
        _contentText = input.Text + @"<br />";
        input.Text = String.Empty;
        DisplayContent.Text = this.ContentText;


The third part of the code example is the Web page. Notice that the <asp:catalogzone> element for the page contains declarations for two CatalogPart controls. These controls become part of a custom CatalogPartCollection object that is created when the Button1_Click method executes. The PageCatalogPart control contains Web server controls that have been previously closed by a user at run time. Controls in the PageCatalogPart control can be added back to a page. The DeclarativeCatalogPart control contains a declaration of the custom TextDisplayWebPart control. When the page is in catalog mode, a user can add the TextDisplayWebPart control to the page so that it can be used in normal browse mode.

<%@ Page Language="C#" %>
<%@ register TagPrefix="uc1" 
  Src="DisplayModeMenuCS.ascx" %>
<%@ Register TagPrefix="aspSample"
  Assembly="TextDisplayWebPartCS" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">
  protected void Button1_Click(object sender, EventArgs e)
    ArrayList list = new ArrayList(2);
    // Pass an ICollection object to the constructor.
    CatalogPartCollection myParts = new CatalogPartCollection(list);
    foreach (CatalogPart catalog in myParts)
      catalog.Description = "My " + catalog.DisplayTitle;

    // Use the IndexOf property to locate a CatalogPart control.
    int PageCatalogPartIndex = myParts.IndexOf(PageCatalogPart1);
    myParts[PageCatalogPartIndex].ChromeType = PartChromeType.TitleOnly;

    // Use the Contains method to see if a CatalogPart control exists.
    if (myParts.Contains(PageCatalogPart1))
      WebPart closedWebPart = null;
      WebPartDescriptionCollection descriptions = PageCatalogPart1.GetAvailableWebPartDescriptions();
      if (descriptions.Count > 0)
        closedWebPart = PageCatalogPart1.GetWebPart(descriptions[0]);
        closedWebPart.AllowClose = false;

    // Use indexers to display the details of the CatalogPart controls.
    Label1.Text = String.Empty;
    Label1.Text =
      "<h3>PageCatalogPart Details</h3>" +
      "ID: " + myParts[0].ID + "<br />" +
      "Count: " + myParts[0].GetAvailableWebPartDescriptions().Count;
    Label1.Text += 
      "<h3>DeclarativeCatalogPart Details</h3>" +
      "ID: " + myParts["DeclarativeCatalogPart1"].ID + "<br />" +
      "Count: " + myParts["DeclarativeCatalogPart1"].GetAvailableWebPartDescriptions().Count;

<html xmlns="" >
<head id="Head1" runat="server">
    <title>PageCatalogPart Details</title>
  <form id="form1" runat="server">
    <asp:WebPartManager ID="WebPartManager1" runat="server" />
    <uc1:DisplayModeMenuCS ID="DisplayModeMenu1" runat="server" />
    <asp:WebPartZone ID="WebPartZone1" runat="server">
          Title="Favorite Links" >
          <asp:ListItem Value="">
          <asp:ListItem Value="">
          <asp:ListItem Value="">
    <asp:CatalogZone ID="CatalogZone1" runat="server">
        <asp:DeclarativeCatalogPart ID="DeclarativeCatalogPart1" runat="server">
            <aspSample:TextDisplayWebPart runat="server" 
              Title="Text Display WebPart" />
        <asp:PageCatalogPart ID="PageCatalogPart1" runat="server" />  
    <hr />
    <asp:Button ID="Button1" 
      Text="Display CatalogPart Properties" 
    <br />
    <asp:Label ID="Label1" runat="server" Text="" /> 

When you load the page in a browser, you can switch the page into catalog mode by selecting Catalog in the Display Mode drop-down list control. You can add the custom WebPart control to the page by selecting the check box next to it and clicking Add. Click Close to return the page to browse mode. On the control you just added, if you click the verbs menu (the down arrow that appears in the title bar) and then click Close, the control is removed from the page and added to the PageCatalogPart control. Return the page to catalog mode, and click the Page Catalog link to view the contents of the PageCatalogPart control. Notice that the control you closed now appears there. Clicking the Display CatalogPart Properties button accesses the CatalogPartCollection object and displays certain properties of the contained CatalogPart controls.

.NET Framework
Available since 2.0

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

Return to top