Export (0) Print
Expand All

WebPartCollection.Item Property (Int32)

Returns a member of the collection based on its position in the collection.

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

public WebPart this[
	int index
] { get; }

Parameters

index
Type: System.Int32

The index of a particular WebPart control in a collection.

Property Value

Type: System.Web.UI.WebControls.WebParts.WebPart
A WebPart at the specified index in the collection.

The Item indexer allows you to access the underlying WebPart controls in a WebPartCollection object by index, and change their property values or call methods.

The following code example demonstrates the use of the Item indexer 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 Button2_Click method creates an empty WebPartCollection object, then assigns to it the WebPart controls from the WebPartZone1.WebParts property. The method accesses the first control in the collection by using its index, and toggles its Title property value.

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

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 can be treated as WebPart controls at run time and made part of a 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>

After you load the page in a browser, click the Toggle BulletedList1 Title button, and notice that the code toggles the title of the control between the two available title choices.

.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