Editare

Partajați prin


WebPartCollection Constructors

Definition

Initializes a new instance of the WebPartCollection class.

Overloads

WebPartCollection()

Initializes an empty new instance of the WebPartCollection class.

WebPartCollection(ICollection)

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

WebPartCollection()

Initializes an empty new instance of the WebPartCollection class.

public:
 WebPartCollection();
public WebPartCollection ();
Public Sub New ()

Remarks

The WebPartCollection constructor initializes an empty instance of the WebPartCollection class. The object itself is read-only and has no method to add individual WebPart controls to it; therefore, you will have little occasion to use this constructor.

See also

Applies to

WebPartCollection(ICollection)

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

public:
 WebPartCollection(System::Collections::ICollection ^ webParts);
public WebPartCollection (System.Collections.ICollection webParts);
new System.Web.UI.WebControls.WebParts.WebPartCollection : System.Collections.ICollection -> System.Web.UI.WebControls.WebParts.WebPartCollection
Public Sub New (webParts As ICollection)

Parameters

webParts
ICollection

An ICollection of WebPart controls.

Exceptions

webParts is null.

An object in the webParts collection is null.

-or-

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

Examples

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.

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";
  }
}
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts

Partial Public Class webpartcollectionvb

  Inherits System.Web.UI.Page

  Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim partCollection As WebPartCollection = mgr1.WebParts
    Dim part As WebPart

    For Each part In partCollection
      If part.ChromeState <> PartChromeState.Minimized Then
        part.ChromeState = PartChromeState.Minimized
      Else
        part.ChromeState = PartChromeState.Normal
      End If
    Next

  End Sub

  Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim partCollection As WebPartCollection = WebPartZone1.WebParts

    If partCollection(0).Title = "My Link List" Then
      partCollection(0).Title = "Favorite Links"
    Else
      partCollection(0).Title = "My Link List"
    End If

  End Sub

End Class

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>
<%@ Page Language="vb"
  Codefile="webpartcollection.vb" 
  Inherits="webpartcollectionvb" %>

<!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 id="Head1" 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, 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.

Remarks

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.

See also

Applies to