Export (0) Print
Expand All

WebPartCollection Class

Contains a collection of WebPart controls used to track and manage related groups of controls. This class cannot be inherited.

System.Object
  System.Collections.ReadOnlyCollectionBase
    System.Web.UI.WebControls.WebParts.WebPartCollection

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

public sealed class WebPartCollection : ReadOnlyCollectionBase

The WebPartCollection type exposes the following members.

  NameDescription
Public methodWebPartCollection()Initializes an empty new instance of the WebPartCollection class.
Public methodWebPartCollection(ICollection)Initializes a new instance of a WebPartCollection object by passing in an ICollection collection of WebPart controls.
Top

  NameDescription
Public propertyCountGets the number of elements contained in the ReadOnlyCollectionBase instance. (Inherited from ReadOnlyCollectionBase.)
Public propertyItem[Int32]Returns a member of the collection based on its position in the collection.
Public propertyItem[String]Returns a member of the collection based on a unique string identifier.
Top

  NameDescription
Public methodContainsReturns a value indicating whether a particular control exists in the collection.
Public methodCopyToCopies the collection to an array of WebPart objects.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetEnumeratorReturns an enumerator that iterates through the ReadOnlyCollectionBase instance. (Inherited from ReadOnlyCollectionBase.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIndexOfReturns the position of a particular member of the collection.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Explicit interface implemetationPrivate methodICollection.CopyToCopies the entire ReadOnlyCollectionBase to a compatible one-dimensional Array, starting at the specified index of the target array. (Inherited from ReadOnlyCollectionBase.)
Explicit interface implemetationPrivate propertyICollection.IsSynchronizedGets a value indicating whether access to a ReadOnlyCollectionBase object is synchronized (thread safe). (Inherited from ReadOnlyCollectionBase.)
Explicit interface implemetationPrivate propertyICollection.SyncRootGets an object that can be used to synchronize access to a ReadOnlyCollectionBase object. (Inherited from ReadOnlyCollectionBase.)
Top

The WebPartCollection class is a read-only collection of controls, typically used by WebPartZoneBase and WebPartManager controls to manage sets of WebPart controls.

A WebPartManager control uses a WebPartCollection object to keep a list of all the WebPart controls on a page, whereas a WebPartZoneBase control uses a WebPartCollection object to track the WebPart controls that it contains.

NoteNote

The WebPartCollection collection contains both WebPart controls and other server controls (such as user controls, custom controls, and ASP.NET controls) that are placed in WebPartZoneBase zones and used as part of Web Parts applications. So, for example, if you have a WebPartZone zone in a page, and within it you declare a custom WebPart control and the ASP.NET Calendar control, both controls will be in the WebPartCollection collection referenced by the WebParts property.

The WebPartCollection object exists so that the Web Parts control set can work with strongly typed collections. Similarly, if you want to perform mass operations on a set of WebPart controls, you can get a reference to a WebPartCollection object using the WebParts property. For example, you might want to loop through all the WebPart controls on a page and change their appearance in some way. Even though the WebPartCollection object is read-only, you can make programmatic changes to the properties of the underlying controls referenced in the collection.

The following code example demonstrates the use of the WebPartCollection object on a Web Parts page. This example has three parts:

  • The code for the page in a partial class.

  • The Web page that contains the controls.

  • A description of how the example works in a browser.

The first part of the code example contains the code for the page in a partial class. Note that the Button1_Click method creates a WebPartCollection object that consists of all the WebPart controls referenced in the WebPartManager.WebParts property, which includes all the WebPart controls on the page. The method iterates through all the controls, and toggles each control's ChromeState property, which determines whether that control is normal or minimized.

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

public partial class webpartcollectioncs : System.Web.UI.Page
{
  protected void Button1_Click(object sender, EventArgs e)
  {

    WebPartCollection partCollection = mgr1.WebParts;

    foreach (WebPart part in partCollection)
    {
      if (part.ChromeState != PartChromeState.Minimized)
        part.ChromeState = PartChromeState.Minimized;
      else
        part.ChromeState = PartChromeState.Normal;
    }
  }
  protected void Button2_Click(object sender, EventArgs e)
  {
    WebPartCollection partCollection = WebPartZone1.WebParts;

    if (partCollection[0].Title == "My Link List")
      partCollection[0].Title = "Favorite Links";
    else
      partCollection[0].Title = "My Link List";
  }
}

The second part of the code example is the Web page that contains the controls. Notice that the controls declared in WebPartZone1 are standard ASP.NET server controls, but because they are wrapped as GenericWebPart controls at run time, and the GenericWebPart class inherits from the WebPart class, the controls are automatically treated as WebPart controls at run time and therefore are included in the WebPartCollection object.

<%@ Page Language="C#" 
  Codefile="webpartcollection.cs" 
  Inherits="webpartcollectioncs" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="mgr1" 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>
          <br />
          <asp:Calendar ID="Calendar1" runat="server" 
            Title="My Calendar" />
        </ZoneTemplate>
      </asp:WebPartZone>
    </div>
    <hr />
    <asp:Button ID="Button1" runat="server" Width="200"
      Text="Toggle ChromeState" OnClick="Button1_Click" />
    <br />
    <asp:Button ID="Button2" runat="server" Width="200"
        Text="Toggle BulletedList1 Title" 
        OnClick="Button2_Click"/>
    </form>
</body>
</html>

After you load the page in a browser and click the Toggle ChromeState button, the code in the partial class loops through the WebPartCollection object and alternately minimizes the controls or returns them to normal. Or, if you repeatedly click the Toggle BulletedList1 Title button, the title of the uppermost control is changed to alternate values.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

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