Export (0) Print
Expand All

WebPartManager.WebParts Property

Note: This property is new in the .NET Framework version 2.0.

Gets a reference to all WebPart controls tracked by the WebPartManager control on a Web page.

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

public WebPartCollection WebParts { get; }
/** @property */
public WebPartCollection get_WebParts ()

public function get WebParts () : WebPartCollection

Property Value

A WebPartCollection that contains references to a set of WebPart controls.

The WebParts property is used by the WebPartManager control to track all WebPart controls that are contained within WebPartZoneBase zones on the page. Although the collection is read-only, you can access individual WebPart controls through the collection and make changes to them programmatically.

NoteNote

It is possible to have a WebPart control placed on a page outside of a WebPartZone zone, which results in the control not being tracked by the WebPartManager control or referenced in its WebParts collection. However, there is little reason to use a WebPart control outside of a zone, because it then loses its Web Parts functionality and acts as a normal server control.

Any type of control that can be placed in a zone, whether a custom WebPart control, a standard ASP.NET control, a user control, or a custom server control, can be treated as a WebPart control at run time. When a control that is not a WebPart control is placed in a WebPartZone zone, at run time ASP.NET wraps the control with a GenericWebPart object, so that the control can behave as a true WebPart control. Therefore, by using the WebParts property, the WebPartManager control can track any type of server control, regardless of whether it derives from the WebPart class.

The following code example demonstrates how to use the WebParts property programmatically to access individual WebPart controls. Notice that in the declarative markup for the Web page, within the <asp:webpartzone> element there are two standard ASP.NET server controls. Although these do not inherit from the WebPart class, because they are in a zone they will be wrapped with a GenericWebPart object at run time, and thus will be included in the collection referenced by the WebParts property. You could also add custom WebPart controls, user controls, or custom server controls in this zone, and they would be handled the same way.

<%@ Page Language="C#" %>

<script runat="server">

  protected void Button1_Click(object sender, EventArgs e)
  {
    Label1.Text = "WebPart count:  " +
      WebPartManager1.WebParts.Count.ToString();
  }

  protected void Button2_Click(object sender, EventArgs e)
  {
    WebPartManager1.WebParts["Calendar1"].ChromeType = 
      PartChromeType.BorderOnly;
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
</head>
<body>
    <form id="form1" runat="server">
      <!-- Reference the WebPartManager control. -->
    <asp:WebPartManager ID="WebPartManager1" runat="server" />    
    <div>
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <asp:BulletedList 
            DisplayMode="HyperLink" 
            ID="BulletedList1" 
            runat="server"
            Title="My Links"
            ExportMode="All">
            <asp:ListItem Value="http://www.microsoft.com">
            Microsoft
            </asp:ListItem>
            <asp:ListItem Value="http://www.msn.com">
            MSN
            </asp:ListItem>
            <asp:ListItem Value="http://www.contoso.com">
            Contoso Corp.
            </asp:ListItem>
          </asp:BulletedList>
          <asp:Calendar ID="Calendar1" runat="server" 
            Title="My Calendar" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <hr />
      <asp:Button ID="Button1" runat="server" 
        Text="WebPart Count" 
        OnClick="Button1_Click" />
      <asp:Button ID="Button2" runat="server" 
        Text="Hide Calendar Title" 
        OnClick="Button2_Click" />  
      <br />
      <asp:Label ID="Label1" runat="server" text="" />
    </div>
    </form>
</body>
</html>

Note that for the code example to work, you must add a setting in the Web.config file to enable exporting Web Parts description files. Ensure that you have a Web.config file in the same directory as the Web page for this code example. Within the <system.web> section, make sure there is a <webParts> element with an enableExport attribute set to true, as in the following markup.

<webParts enableExport="true">

...

</webParts>

After you load the page into a browser, if you click the WebPart Count button, the code uses the WebParts property to return the count of controls in the collection. If you click the Hide Calendar Title button, the code changes the calendar so that it renders with only a border and not a title.

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.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft