WebPartManager.WebParts Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan referensi ke semua WebPart kontrol yang WebPartManager dilacak oleh kontrol pada halaman Web.
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
Nilai Properti
WebPartCollection yang berisi referensi ke sekumpulan WebPart kontrol.
- Atribut
Contoh
Contoh kode berikut menunjukkan cara menggunakan WebParts properti secara terprogram untuk mengakses kontrol individual WebPart . Perhatikan bahwa dalam markup deklaratif untuk halaman Web, dalam <asp:webpartzone> elemen ada dua kontrol server ASP.NET standar. Meskipun ini tidak mewarisi dari WebPart kelas , karena mereka berada di zona mereka akan dibungkus dengan GenericWebPart objek pada waktu proses, dan dengan demikian akan disertakan dalam koleksi yang direferensikan oleh WebParts properti . Anda juga dapat menambahkan kontrol kustom WebPart , kontrol pengguna, atau kontrol server kustom di zona ini, dan kontrol tersebut akan ditangani dengan cara yang sama.
<%@ 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>
Perhatikan bahwa agar contoh kode berfungsi, Anda harus menambahkan pengaturan dalam file Web.config untuk mengaktifkan ekspor file deskripsi Komponen Web. Pastikan Anda memiliki file Web.config di direktori yang sama dengan halaman Web untuk contoh kode ini. Di dalam bagian <system.web> , pastikan ada <webParts> elemen dengan atribut yang enableExport diatur ke true, seperti dalam markup berikut.
<webParts enableExport="true">
...
</webParts>
Setelah Anda memuat halaman ke browser, jika Anda mengklik tombol Jumlah WebPart , kode menggunakan WebParts properti untuk mengembalikan jumlah kontrol dalam koleksi. Jika Anda mengklik tombol Sembunyikan Judul Kalender , kode akan mengubah kalender sehingga hanya dirender dengan batas dan bukan judul.
Keterangan
Properti WebParts digunakan oleh WebPartManager kontrol untuk melacak semua WebPart kontrol yang terkandung dalam WebPartZoneBase zona pada halaman. Meskipun koleksi bersifat baca-saja, Anda dapat mengakses kontrol individual WebPart melalui koleksi dan membuat perubahan pada koleksi tersebut secara terprogram.
Nota
Dimungkinkan untuk memiliki kontrol yang WebPart ditempatkan pada halaman di luar WebPartZone zona, yang menghasilkan kontrol tidak dilacak oleh WebPartManager kontrol atau direferensikan dalam koleksinya WebParts . Namun, ada sedikit alasan untuk menggunakan WebPart kontrol di luar zona, karena kemudian kehilangan fungsionalitas Bagian Web dan bertindak sebagai kontrol server normal.
Semua jenis kontrol yang dapat ditempatkan di zona, baik kontrol kustom WebPart , kontrol ASP.NET standar, kontrol pengguna, atau kontrol server kustom, dapat diperlakukan sebagai WebPart kontrol pada waktu proses. Ketika kontrol yang bukan WebPart kontrol ditempatkan di WebPartZone zona, pada waktu proses ASP.NET membungkus kontrol dengan GenericWebPart objek, sehingga kontrol dapat berperilaku sebagai kontrol sejati WebPart . Oleh karena itu, dengan menggunakan WebParts properti , WebPartManager kontrol dapat melacak semua jenis kontrol server, terlepas dari apakah itu berasal dari WebPart kelas .