Bagikan melalui


WebPartCollection Kelas

Definisi

Berisi kumpulan WebPart kontrol yang digunakan untuk melacak dan mengelola grup kontrol terkait. Kelas ini tidak dapat diwariskan.

public ref class WebPartCollection sealed : System::Collections::ReadOnlyCollectionBase
public sealed class WebPartCollection : System.Collections.ReadOnlyCollectionBase
type WebPartCollection = class
    inherit ReadOnlyCollectionBase
Public NotInheritable Class WebPartCollection
Inherits ReadOnlyCollectionBase
Warisan
WebPartCollection

Contoh

Contoh kode berikut menunjukkan penggunaan WebPartCollection objek pada halaman Bagian Web. Contoh ini memiliki tiga bagian:

  • Kode untuk halaman di kelas parsial.

  • Halaman Web yang berisi kontrol.

  • Deskripsi tentang cara kerja contoh di browser.

Bagian pertama dari contoh kode berisi kode untuk halaman di kelas parsial. Perhatikan bahwa Button1_Click metode membuat WebPartCollection objek yang terdiri dari semua WebPart kontrol yang dirujuk dalam WebPartManager.WebParts properti , yang mencakup semua WebPart kontrol di halaman. Metode ini melakukan iterasi melalui semua kontrol, dan beralih ke properti setiap kontrol ChromeState , yang menentukan apakah kontrol tersebut normal atau diminimalkan.

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

Bagian kedua dari contoh kode adalah halaman Web yang berisi kontrol. Perhatikan bahwa kontrol yang dideklarasikan dalam WebPartZone1 adalah kontrol server ASP.NET standar, tetapi karena dibungkus sebagai GenericWebPart kontrol pada durasi, dan GenericWebPart kelas mewarisi dari WebPart kelas, kontrol secara otomatis diperlakukan sebagai WebPart kontrol pada durasi dan oleh karena itu disertakan dalam WebPartCollection objek .

<%@ 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>

Setelah Anda memuat halaman di browser dan mengeklik tombol Alihkan ChromeState , kode di kelas parsial mengulangi WebPartCollection objek dan secara bergantian meminimalkan kontrol atau mengembalikannya ke normal. Atau, jika Anda berulang kali mengklik tombol Alihkan Judul BulletedList1 , judul kontrol paling atas diubah menjadi nilai alternatif.

Keterangan

Kelas WebPartCollection ini adalah kumpulan kontrol baca-saja, biasanya digunakan oleh WebPartZoneBase dan WebPartManager kontrol untuk mengelola serangkaian WebPart kontrol.

WebPartManager Kontrol menggunakan WebPartCollection objek untuk menyimpan daftar semua WebPart kontrol pada halaman, sedangkan WebPartZoneBase kontrol menggunakan WebPartCollection objek untuk melacak WebPart kontrol yang dikandungnya.

Catatan

Koleksi WebPartCollection berisi WebPart kontrol dan kontrol server lainnya (seperti kontrol pengguna, kontrol kustom, dan kontrol ASP.NET) yang ditempatkan di WebPartZoneBase zona dan digunakan sebagai bagian dari aplikasi Bagian Web. Jadi, misalnya, jika Anda memiliki WebPartZone zona di halaman, dan di dalamnya Anda mendeklarasikan kontrol kustom WebPart dan kontrol ASP.NET Calendar , kedua kontrol akan berada dalam koleksi yang WebPartCollection direferensikan oleh WebParts properti .

Objek WebPartCollection ada sehingga kumpulan kontrol Bagian Web bisa bekerja dengan koleksi yang diketik dengan kuat. Demikian pula, jika Anda ingin melakukan operasi massal pada serangkaian WebPart kontrol, Anda bisa mendapatkan referensi ke WebPartCollection objek menggunakan WebParts properti . Misalnya, Anda mungkin ingin mengulang semua WebPart kontrol di halaman dan mengubah tampilannya dalam beberapa cara. Meskipun WebPartCollection objek bersifat baca-saja, Anda dapat membuat perubahan terprogram pada properti kontrol dasar yang dirujuk dalam koleksi.

Konstruktor

WebPartCollection()

Menginisialisasi instans WebPartCollection baru kelas yang kosong.

WebPartCollection(ICollection)

Menginisialisasi instans WebPartCollection baru objek dengan meneruskan ICollection kumpulan WebPart kontrol.

Properti

Count

Mendapatkan jumlah elemen yang terkandung dalam ReadOnlyCollectionBase instans.

(Diperoleh dari ReadOnlyCollectionBase)
InnerList

Mendapatkan daftar elemen yang terkandung dalam ReadOnlyCollectionBase instans.

(Diperoleh dari ReadOnlyCollectionBase)
Item[Int32]

Mengembalikan anggota koleksi berdasarkan posisinya dalam koleksi.

Item[String]

Mengembalikan anggota koleksi berdasarkan pengidentifikasi string unik.

Metode

Contains(WebPart)

Mengembalikan nilai yang menunjukkan apakah kontrol tertentu ada dalam koleksi.

CopyTo(WebPart[], Int32)

Menyalin koleksi ke array WebPart objek.

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
GetEnumerator()

Mengembalikan enumerator yang melakukan iterasi melalui ReadOnlyCollectionBase instans.

(Diperoleh dari ReadOnlyCollectionBase)
GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetType()

Mendapatkan instans Type saat ini.

(Diperoleh dari Object)
IndexOf(WebPart)

Mengembalikan posisi anggota koleksi tertentu.

MemberwiseClone()

Membuat salinan dangkal dari yang saat ini Object.

(Diperoleh dari Object)
ToString()

Mengembalikan string yang mewakili objek saat ini.

(Diperoleh dari Object)

Implementasi Antarmuka Eksplisit

ICollection.CopyTo(Array, Int32)

Menyalin seluruh ReadOnlyCollectionBase ke satu dimensi Arrayyang kompatibel, dimulai dari indeks array target yang ditentukan.

(Diperoleh dari ReadOnlyCollectionBase)
ICollection.IsSynchronized

Mendapatkan nilai yang menunjukkan apakah akses ke ReadOnlyCollectionBase objek disinkronkan (utas aman).

(Diperoleh dari ReadOnlyCollectionBase)
ICollection.SyncRoot

Mendapatkan objek yang dapat digunakan untuk menyinkronkan akses ke ReadOnlyCollectionBase objek.

(Diperoleh dari ReadOnlyCollectionBase)

Metode Ekstensi

Cast<TResult>(IEnumerable)

Mentransmisikan elemen dari IEnumerable ke jenis yang ditentukan.

OfType<TResult>(IEnumerable)

Memfilter elemen berdasarkan IEnumerable jenis tertentu.

AsParallel(IEnumerable)

Mengaktifkan paralelisasi kueri.

AsQueryable(IEnumerable)

Mengonversi menjadi IEnumerableIQueryable.

Berlaku untuk

Lihat juga