WebPartCollection.Item[] 属性

定义

根据位置或唯一标识符返回集合中的特定成员。

重载

Item[Int32]

基于集合中的位置返回集合的成员。

Item[String]

基于唯一字符串标识符返回集合的成员。

Item[Int32]

基于集合中的位置返回集合的成员。

public:
 property System::Web::UI::WebControls::WebParts::WebPart ^ default[int] { System::Web::UI::WebControls::WebParts::WebPart ^ get(int index); };
public System.Web.UI.WebControls.WebParts.WebPart this[int index] { get; }
member this.Item(int) : System.Web.UI.WebControls.WebParts.WebPart
Default Public ReadOnly Property Item(index As Integer) As WebPart

参数

index
Int32

集合中特定的 WebPart 控件的索引。

属性值

WebPart

位于集合中指定索引处的 WebPart

示例

下面的代码示例演示如何在Web 部件页上使用该Item[]索引器。 此示例有三个部分:

  • 分部类中页面的代码。

  • 包含控件的网页。

  • 说明示例在浏览器中的工作原理。

代码示例的第一部分包含分部类中页面的代码。 请注意,该方法 Button2_Click 创建一个空 WebPartCollection 对象,然后从属性分配给该 WebPart 对象 WebPartZone1.WebParts 。 该方法使用其索引访问集合中的第一个控件,并切换其 Title 属性值。

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";
  }
}
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts

Partial Public Class webpartcollectionvb

  Inherits System.Web.UI.Page

  Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim partCollection As WebPartCollection = mgr1.WebParts
    Dim part As WebPart

    For Each part In partCollection
      If part.ChromeState <> PartChromeState.Minimized Then
        part.ChromeState = PartChromeState.Minimized
      Else
        part.ChromeState = PartChromeState.Normal
      End If
    Next

  End Sub

  Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim partCollection As WebPartCollection = WebPartZone1.WebParts

    If partCollection(0).Title = "My Link List" Then
      partCollection(0).Title = "Favorite Links"
    Else
      partCollection(0).Title = "My Link List"
    End If

  End Sub

End Class

代码示例的第二部分是包含控件的网页。 请注意,声明的WebPartZone1控件是标准 ASP.NET 服务器控件,但由于它们在运行时包装为GenericWebPart控件,GenericWebPart并且类继承自WebPart该类,因此可以在运行时将控件视为WebPart控件,并成为对象的一WebPartCollection部分。

<%@ 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>
<%@ Page Language="vb"
  Codefile="webpartcollection.vb" 
  Inherits="webpartcollectionvb" %>

<!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 id="Head1" 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>

在浏览器中加载页面后,单击 “切换项目符号列表1 标题 ”按钮,并注意到代码在两个可用游戏选项之间切换控件的标题。

注解

索引Item[]器允许按索引访问对象中WebPartCollection的基础WebPart控件,并更改其属性值或调用方法。

另请参阅

适用于

Item[String]

基于唯一字符串标识符返回集合的成员。

public:
 property System::Web::UI::WebControls::WebParts::WebPart ^ default[System::String ^] { System::Web::UI::WebControls::WebParts::WebPart ^ get(System::String ^ id); };
public System.Web.UI.WebControls.WebParts.WebPart this[string id] { get; }
member this.Item(string) : System.Web.UI.WebControls.WebParts.WebPart
Default Public ReadOnly Property Item(id As String) As WebPart

参数

id
String

集合中特定 WebPart 控件的唯一标识符。

属性值

WebPart

集合中 ID 等于 id 的值的第一个 WebPart

注解

索引 Item[] 器使你能够根据唯一 WebPart 标识符访问对象中的 WebPartCollection 控件。

备注

Web 部件控件集对此属性执行不区分大小写的匹配,因此区分大小写不是唯id一值的一部分。

Item[] 属性还可用于识别某些专用情况下对象的成员 WebPartCollection 。 对于 GenericWebPart 控件,索引器能够匹配由控件包装 GenericWebPart 的基础子控件的标识符。 对于ProxyWebPart控件,索引器根据参数和OriginalIDGenericWebPartID属性值的不区分大小写的比较来匹配控件的id标识符。

另请参阅

适用于