This documentation is archived and is not being maintained.

WebPartCollection Constructor (ICollection)

Initializes a new instance of a WebPartCollection object by passing in an ICollection collection of WebPart controls.

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

public:
WebPartCollection(
	ICollection^ webParts
)

Parameters

webParts
Type: System.Collections::ICollection
An ICollection of WebPart controls.

ExceptionCondition
ArgumentNullException

webParts is nullptr.

ArgumentException

An object in the webParts collection is nullptr.

- or -

An object in the webParts collection is not a of type WebPart.

The WebPartCollection constructor initializes an instance of the WebPartCollection class by passing in a collection of WebPart controls.

Although a WebPartCollection object is read-only and there is no method for adding individual controls to it, you can create your own ICollection collection of controls, and pass that to the WebPartCollection constructor. This enables you to create custom collections and perform bulk operations on them. You can also access the underlying controls in the collection and change their property values programmatically.

The following code example demonstrates the use of the WebPartCollection constructor 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.

No code example is currently available or this language may not be supported.

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.

No code example is currently available or this language may not be supported.

After you load the page in a browser, click the Toggle ChromeState button, and notice that 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, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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