Export (0) Print
Expand All

WebPartManager.WebParts Property

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)

[BrowsableAttribute(false)]
public WebPartCollection WebParts { get; }

Property Value

Type: System.Web.UI.WebControls.WebParts.WebPartCollection
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#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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">
    <title>ASP.NET Example</title>
</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.

.NET Framework

Supported in: 4.5.2, 4.5.1, 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.

Show:
© 2014 Microsoft