WebPartManager.WebParts Property

Definition

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

public:
 property System::Web::UI::WebControls::WebParts::WebPartCollection ^ WebParts { System::Web::UI::WebControls::WebParts::WebPartCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Web.UI.WebControls.WebParts.WebPartCollection WebParts { get; }
[<System.ComponentModel.Browsable(false)>]
member this.WebParts : System.Web.UI.WebControls.WebParts.WebPartCollection
Public ReadOnly Property WebParts As WebPartCollection

Property Value

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

Attributes

Examples

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>
<%@ Page Language="vb" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Protected Sub Button1_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    
    Label1.Text = "WebPart count:  " & _
      WebPartManager1.WebParts.Count.ToString()
  End Sub

  Protected Sub Button2_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    
    WebPartManager1.WebParts("Calendar1").ChromeType = _
      PartChromeType.BorderOnly

  End Sub
</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.

Remarks

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.

Note

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.

Applies to