WebPartCollection.Item[] 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.
Mengembalikan anggota koleksi tertentu sesuai dengan posisinya atau pengidentifikasi unik.
Overload
Item[Int32] |
Mengembalikan anggota koleksi berdasarkan posisinya dalam koleksi. |
Item[String] |
Mengembalikan anggota koleksi berdasarkan pengidentifikasi string unik. |
Item[Int32]
Mengembalikan anggota koleksi berdasarkan posisinya dalam koleksi.
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
Parameter
Nilai Properti
pada WebPart indeks yang ditentukan dalam koleksi.
Contoh
Contoh kode berikut menunjukkan penggunaan Item[] pengindeks 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 Button2_Click
metode membuat objek kosong WebPartCollection , lalu menetapkan WebPart kontrol dari WebPartZone1.WebParts
properti. Metode ini mengakses kontrol pertama dalam koleksi dengan menggunakan indeksnya, dan mengalihkan nilai propertinya 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
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 dapat diperlakukan sebagai WebPart kontrol pada durasi dan menjadi bagian WebPartCollection dari 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, klik tombol Alihkan Judul BulletedList1 , dan perhatikan bahwa kode mengalihkan judul kontrol antara dua pilihan judul yang tersedia.
Keterangan
Pengindeks Item[] memungkinkan Anda mengakses kontrol yang mendasar WebPart dalam WebPartCollection objek menurut indeks, dan mengubah nilai properti atau metode panggilannya.
Lihat juga
Berlaku untuk
Item[String]
Mengembalikan anggota koleksi berdasarkan pengidentifikasi string unik.
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
Parameter
Nilai Properti
Yang pertama WebPart dalam koleksi yang ID-nya sama dengan nilai id
.
Keterangan
Pengindeks Item[] memungkinkan Anda mengakses WebPart kontrol dalam WebPartCollection objek sesuai dengan pengidentifikasi unik.
Catatan
Set kontrol Bagian Web melakukan pencocokan tidak peka huruf besar/kecil pada properti ini, sehingga sensitivitas huruf besar/kecil bukan bagian dari nilai unik id
.
Properti Item[] ini juga berfungsi untuk mengidentifikasi anggota WebPartCollection objek dalam beberapa kasus khusus. Dalam kasus GenericWebPart kontrol, pengindeks dapat mencocokkan pengidentifikasi untuk kontrol anak yang mendasar yang GenericWebPart dibungkus oleh kontrol. Dalam kasus ProxyWebPart kontrol, pengindeks cocok dengan pengidentifikasi untuk kontrol berdasarkan perbandingan id
parameter yang tidak peka huruf besar/kecil dan nilai OriginalID properti atau GenericWebPartID .