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

範例

下列程式碼範例示範在網頁元件頁面上使用 Item[] 索引子。 此範例有三個部分:

  • 部分類別中頁面的程式碼。

  • 包含控制項的網頁。

  • 範例在瀏覽器中運作方式的描述。

程式碼範例的第一個部分包含部分類別中頁面的程式碼。 請注意, Button2_Click 方法會建立空 WebPartCollection 的 物件,然後從 WebPartZone1.WebParts 屬性將控制項指派給它 WebPart 。 方法會使用其索引來存取集合中的第一個控制項,並切換其 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 控制項的唯一識別碼。

屬性值

在集合中,ID 等於 id 值的第一個 WebPart

備註

索引 Item[] 器可讓您根據唯一識別碼存取 WebPart 物件中的 WebPartCollection 控制項。

注意

Web 元件控制項集會在此屬性上執行不區分大小寫的比對,因此區分大小寫不是唯 id 一值的一部分。

屬性 Item[] 也適用于在某些特殊案例中識別物件的成員 WebPartCollection 。 在控制項的情況下 GenericWebPart ,索引子能夠比對控制項所包裝之基礎子控制項的 GenericWebPart 識別碼。 在控制項的情況下 ProxyWebPart ,索引子會根據參數和 或 GenericWebPartID 屬性值的不區分大小寫比較 id ,比對控制項的 OriginalID 識別碼。

另請參閱

適用於