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)

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.


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"
<script runat="server">

  protected void Button1_Click(object sender, EventArgs e)
    Label1.Text = "WebPart count:  " +

  protected void Button2_Click(object sender, EventArgs e)
    WebPartManager1.WebParts["Calendar1"].ChromeType = 

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>ASP.NET Example</title>
    <form id="form1" runat="server">
      <!-- Reference the WebPartManager control. -->
    <asp:WebPartManager ID="WebPartManager1" runat="server" />    
      <asp:WebPartZone ID="WebPartZone1" runat="server">
            Title="My Links"
            <asp:ListItem Value="http://www.microsoft.com">
            <asp:ListItem Value="http://www.msn.com">
            <asp:ListItem Value="http://www.contoso.com">
            Contoso Corp.
          <asp:Calendar ID="Calendar1" runat="server" 
            Title="My Calendar" />
      <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="" />

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">



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
Available since 2.0
Return to top