CatalogPartCollection Class
Assembly: System.Web (in system.web.dll)
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 Server Control. Note that the control has a property named ContentText; this property holds the value that the user enters in the text box.
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 TextDisplayWebPart Inherits WebPart Private _contentText As String = Nothing Private _fontStyle As String = Nothing Private input As TextBox Private DisplayContent As Label Private lineBreak As Literal <Personalizable(), WebBrowsable()> _ Public Property ContentText() As String Get Return _contentText End Get Set(ByVal value As String) _contentText = value End Set End Property Protected Overrides Sub CreateChildControls() Controls.Clear() DisplayContent = New Label() DisplayContent.BackColor = Color.LightBlue DisplayContent.Text = Me.ContentText Me.Controls.Add(DisplayContent) lineBreak = New Literal() lineBreak.Text = "<br />" Controls.Add(lineBreak) input = New TextBox() Me.Controls.Add(input) Dim update As New Button() update.Text = "Set Label Content" AddHandler update.Click, AddressOf Me.submit_Click Me.Controls.Add(update) End Sub Private Sub submit_Click(ByVal sender As Object, _ ByVal e As EventArgs) ' Update the label string. If input.Text <> String.Empty Then _contentText = input.Text + "<br />" input.Text = String.Empty DisplayContent.Text = Me.ContentText End If End Sub End Class End Namespace
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="vb" %> <%@ register TagPrefix="uc1" TagName="DisplayModeMenuVB" Src="DisplayModeMenuVB.ascx" %> <%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB.Controls" Assembly="TextDisplayWebPartVB" %> <script runat="server"> Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Dim list As New ArrayList(2) list.Add(PageCatalogPart1) list.Add(DeclarativeCatalogPart1) ' Pass an ICollection object to the constructor. Dim myParts As New CatalogPartCollection(list) Dim catalog As CatalogPart For Each catalog In myParts catalog.Description = "My " + catalog.DisplayTitle Next catalog ' Use the IndexOf property to locate a CatalogPart control. Dim PageCatalogPartIndex As Integer = _ myParts.IndexOf(PageCatalogPart1) myParts(PageCatalogPartIndex).ChromeType = PartChromeType.TitleOnly ' Use the Contains method to see if a CatalogPart control exists. If myParts.Contains(PageCatalogPart1) Then Dim closedWebPart As WebPart = Nothing Dim descriptions As WebPartDescriptionCollection = _ PageCatalogPart1.GetAvailableWebPartDescriptions() If descriptions.Count > 0 Then closedWebPart = PageCatalogPart1.GetWebPart(descriptions(0)) closedWebPart.AllowClose = False End If End If ' 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 End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> </head> <body> <form id="form1" runat="server"> <asp:WebPartManager ID="WebPartManager1" runat="server" /> <uc1:DisplayModeMenuVB ID="DisplayModeMenu1" runat="server" /> <asp:WebPartZone ID="WebPartZone1" runat="server"> <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> </ZoneTemplate> </asp:WebPartZone> <asp:CatalogZone ID="CatalogZone1" runat="server"> <ZoneTemplate> <asp:DeclarativeCatalogPart ID="DeclarativeCatalogPart1" runat="server"> <WebPartsTemplate> <aspSample:TextDisplayWebPart runat="server" id="TextDisplayWebPart1" Title="Text Display WebPart" /> </WebPartsTemplate> </asp:DeclarativeCatalogPart> <asp:PageCatalogPart ID="PageCatalogPart1" runat="server" /> </ZoneTemplate> </asp:CatalogZone> <hr /> <asp:Button ID="Button1" runat="server" Text="Display CatalogPart Properties" OnClick="Button1_Click"/> <br /> <asp:Label ID="Label1" runat="server" Text="" /> </form> </body> </html>
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.
- AspNetHostingPermission for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
System.Collections.ReadOnlyCollectionBase
System.Web.UI.WebControls.WebParts.CatalogPartCollection
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.