WebPartZoneCollection Klasa

Definicja

Zawiera kolekcję WebPartZone kontrolek tylko do odczytu.

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

Przykłady

W poniższym przykładzie kodu pokazano użycie WebPartZoneCollection klasy. Przykładowy kod zawiera trzy części: dwie sekcje kodu i wyjaśnienie sposobu uruchamiania przykładu.

Pierwsza część przykładu kodu to strona sieci Web zawierająca trzy WebPartZone kontrolki, z których każda zawiera jedną kontrolkę serwera. Chociaż te kontrolki serwera są standardowymi kontrolkami ASP.NET, mogą pełnić rolę WebPart kontrolek w czasie wykonywania, ponieważ są one umieszczane w WebPartZone strefie.

<%@ Page Language="C#" 
    Codefile="webpartzonecollection_overview.cs" 
    Inherits="webpartzonecollection_overview" %>

<!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>WebPartZoneCollection Demo</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:WebPartManager ID="mgr" runat="server" />
    <asp:Table ID="Table1" runat="server">
      <asp:TableRow>
        <asp:TableCell>
          <asp:Label ID="lblZone1" runat="server" 
            Font-Bold="true"
            AssociatedControlID="WebPartZone1">
            WebPartZone1 Contents
          </asp:Label>
          <asp:WebPartZone ID="WebPartZone1" 
            runat="server" Width="230">
            <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>      
            </ZoneTemplate>
          </asp:WebPartZone>
          <div>
          <asp:Label ID="lblZone2" runat="server" 
            Font-Bold="true"
            AssociatedControlID="WebPartZone2">
            WebPartZone2 Contents
          </asp:Label>
          </div>
          <asp:WebPartZone ID="WebPartZone2" 
            runat="server" Width="230">
            <ZoneTemplate>
              <asp:Calendar ID="Calendar1" 
                runat="server" 
                Title="My Calendar" 
                CatalogIconImageUrl="Mine.gif" />
            </ZoneTemplate>
          </asp:WebPartZone> 
        </asp:TableCell>
        <asp:TableCell VerticalAlign="top"> 
          <asp:Label ID="lblZone3" runat="server" 
            Font-Bold="true" 
            AssociatedControlID="WebPartZone3">
            WebPartZone3 Contents
          </asp:Label>
          <asp:WebPartZone ID="WebPartZone3" runat="server">
            <ZoneTemplate>
              <asp:Table runat="server" ID="table2" 
                Title="Employee Extensions">
                <asp:TableHeaderRow>
                  <asp:TableHeaderCell Scope="Column">
                    Employee Name</asp:TableHeaderCell>
                  <asp:TableHeaderCell Scope="Column">
                    Extension</asp:TableHeaderCell>
                </asp:TableHeaderRow>
                <asp:TableRow>
                  <asp:TableCell>Alberts, Amy</asp:TableCell>
                  <asp:TableCell>x9764</asp:TableCell>
                </asp:TableRow>                
                <asp:TableRow>
                  <asp:TableCell>Hanif, Karim</asp:TableCell>
                  <asp:TableCell>x3240</asp:TableCell>
                </asp:TableRow>
                <asp:TableRow>
                  <asp:TableCell>Penor, Lori</asp:TableCell>
                  <asp:TableCell>x4165</asp:TableCell>
                </asp:TableRow>
              </asp:Table>
            </ZoneTemplate>
          </asp:WebPartZone>
        </asp:TableCell>
      </asp:TableRow>
    </asp:Table>
    </div>
    <hr />
    <asp:Table ID="Table3" runat="server">
      <asp:TableRow>
        <asp:TableCell>
          <asp:Button ID="Button1" runat="server" 
            Width ="200" OnClick="Button1_Click" 
            Text="Total Zone Count" />
          <br />
          <asp:Button ID="Button2" runat="server" 
            Width ="200" OnClick="Button2_Click"
            Text="Coll. Contains WebPartZone2" />
          <br />
          <asp:Button ID="Button3" runat="server" 
            Width ="200" OnClick="Button3_Click"
            Text="Zone Names from Array" />
          <br />
          <asp:Button ID="Button4" runat="server" 
            Width ="200" OnClick="Button4_Click"
            Text="WebPartZone1 Index" />  
          <br />
          <asp:Button ID="Button5" runat="server" 
            Width ="200" OnClick="Button5_Click"
            Text="Toggle Verb Render Mode" />
        </asp:TableCell>
        <asp:TableCell HorizontalAlign="right" 
          Width="200" VerticalAlign="top">
          <asp:Label ID="Label1" runat="server" 
            Font-Bold="true" />
        </asp:TableCell>  
      </asp:TableRow>
    </asp:Table>
    </form>
</body>
</html>
<%@ Page Language="VB" 
    Codefile="webpartzonecollection_overview.vb" 
    Inherits="webpartzonecollection_overview" %>

<!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>WebPartZoneCollection Demo</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:WebPartManager ID="mgr" runat="server" />
    <asp:Table ID="Table1" runat="server">
      <asp:TableRow>
        <asp:TableCell>
          <asp:Label ID="lblZone1" runat="server" 
            Font-Bold="true"
            AssociatedControlID="WebPartZone1">
            WebPartZone1 Contents
          </asp:Label>
          <asp:WebPartZone ID="WebPartZone1" 
            runat="server" Width="230">
            <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>      
            </ZoneTemplate>
          </asp:WebPartZone>
          <div>
          <asp:Label ID="lblZone2" runat="server" 
            Font-Bold="true"
            AssociatedControlID="WebPartZone2">
            WebPartZone2 Contents
          </asp:Label>
          </div>
          <asp:WebPartZone ID="WebPartZone2" 
            runat="server" Width="230">
            <ZoneTemplate>
              <asp:Calendar ID="Calendar1" 
                runat="server" 
                Title="My Calendar" 
                CatalogIconImageUrl="Mine.gif" />
            </ZoneTemplate>
          </asp:WebPartZone> 
        </asp:TableCell>
        <asp:TableCell VerticalAlign="top"> 
          <asp:Label ID="lblZone3" runat="server" 
            Font-Bold="true" 
            AssociatedControlID="WebPartZone3">
            WebPartZone3 Contents
          </asp:Label>
          <asp:WebPartZone ID="WebPartZone3" runat="server">
            <ZoneTemplate>
              <asp:Table runat="server" ID="table2" 
                Title="Employee Extensions">
                <asp:TableHeaderRow>
                  <asp:TableHeaderCell Scope="Column">
                    Employee Name</asp:TableHeaderCell>
                  <asp:TableHeaderCell Scope="Column">
                    Extension</asp:TableHeaderCell>
                </asp:TableHeaderRow>
                <asp:TableRow>
                  <asp:TableCell>Alberts, Amy</asp:TableCell>
                  <asp:TableCell>x9764</asp:TableCell>
                </asp:TableRow>                
                <asp:TableRow>
                  <asp:TableCell>Hanif, Karim</asp:TableCell>
                  <asp:TableCell>x3240</asp:TableCell>
                </asp:TableRow>
                <asp:TableRow>
                  <asp:TableCell>Penor, Lori</asp:TableCell>
                  <asp:TableCell>x4165</asp:TableCell>
                </asp:TableRow>
              </asp:Table>
            </ZoneTemplate>
          </asp:WebPartZone>
        </asp:TableCell>
      </asp:TableRow>
    </asp:Table>
    </div>
    <hr />
    <asp:Table ID="Table3" runat="server">
      <asp:TableRow>
        <asp:TableCell>
          <asp:Button ID="Button1" runat="server" 
            Width ="200" OnClick="Button1_Click" 
            Text="Total Zone Count" />
          <br />
          <asp:Button ID="Button2" runat="server" 
            Width ="200" OnClick="Button2_Click"
            Text="Coll. Contains WebPartZone2" />
          <br />
          <asp:Button ID="Button3" runat="server" 
            Width ="200" OnClick="Button3_Click"
            Text="Zone Names from Array" />
          <br />
          <asp:Button ID="Button4" runat="server" 
            Width ="200" OnClick="Button4_Click"
            Text="WebPartZone1 Index" />  
          <br />
          <asp:Button ID="Button5" runat="server" 
            Width ="200" OnClick="Button5_Click"
            Text="Toggle Verb Render Mode" />
        </asp:TableCell>
        <asp:TableCell HorizontalAlign="right" 
          Width="200" VerticalAlign="top">
          <asp:Label ID="Label1" runat="server" 
            Font-Bold="true" />
        </asp:TableCell>  
      </asp:TableRow>
    </asp:Table>
    </form>
</body>
</html>

Drugą częścią przykładu kodu jest klasa częściowa zawierająca kod do zademonstrowania użycia WebPartZoneCollection klasy. Ten kod obsługuje zdarzenia dla strony sieci Web.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class webpartzonecollection_overview : System.Web.UI.Page
{

  protected void Button1_Click(object sender, EventArgs e)
  {
    Label1.Text = String.Empty;
    Label1.Text = "WebPartZone Count:  " + mgr.Zones.Count;
  }

  protected void Button2_Click(object sender, EventArgs e)
  {
    Label1.Text = String.Empty;
    Label1.Text = mgr.Zones.Contains(WebPartZone2).ToString();
  }

  protected void Button3_Click(object sender, EventArgs e)
  {
    Label1.Text = String.Empty;
    WebPartZoneBase[] zoneArray = new WebPartZoneBase[mgr.Zones.Count];
    mgr.Zones.CopyTo(zoneArray, 0);
    Label1.Text = zoneArray[2].ID;
    Label1.Text += ", " + zoneArray[1].ID;
    Label1.Text += ", " + zoneArray[0].ID;
  }

  protected void Button4_Click(object sender, EventArgs e)
  {
    Label1.Text = String.Empty;
    Label1.Text = "WebPartZone1 index:  " + mgr.Zones.IndexOf(WebPartZone1);
  }

  protected void Button5_Click(object sender, EventArgs e)
  {
    Label1.Text = String.Empty;

    WebPartZoneCollection zoneCollection = mgr.Zones;
    foreach (WebPartZone zone in zoneCollection)
    {

      if (zone.WebPartVerbRenderMode == WebPartVerbRenderMode.Menu)
        zone.WebPartVerbRenderMode = WebPartVerbRenderMode.TitleBar;
      else
        zone.WebPartVerbRenderMode = WebPartVerbRenderMode.Menu;
    }
  }
}
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls


Partial Public Class webpartzonecollection_overview

  Inherits Page

  Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Label1.Text = String.Empty
    Label1.Text = "WebPartZone Count:  " & mgr.Zones.Count

  End Sub


  Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)
    Label1.Text = String.Empty
    Label1.Text = mgr.Zones.Contains(WebPartZone2).ToString()

  End Sub

  Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs)
    Label1.Text = String.Empty
    Dim zoneArray(mgr.Zones.Count) As WebPartZoneBase
    mgr.Zones.CopyTo(zoneArray, 0)
    Label1.Text = zoneArray(2).ID
    Label1.Text += ", " & zoneArray(1).ID
    Label1.Text += ", " & zoneArray(0).ID

  End Sub

  Protected Sub Button4_Click(ByVal sender As Object, ByVal e As EventArgs)
    Label1.Text = String.Empty
    Label1.Text = "WebPartZone1 index:  " & mgr.Zones.IndexOf(WebPartZone1)

  End Sub

  Protected Sub Button5_Click(ByVal sender As Object, ByVal e As EventArgs)
    Label1.Text = String.Empty

    Dim zoneCollection As WebPartZoneCollection = mgr.Zones
    Dim zone As WebPartZone
    For Each zone In zoneCollection
      If zone.WebPartVerbRenderMode = WebPartVerbRenderMode.Menu Then
        zone.WebPartVerbRenderMode = WebPartVerbRenderMode.TitleBar
      Else
        zone.WebPartVerbRenderMode = WebPartVerbRenderMode.Menu
      End If
    Next zone

  End Sub

End Class

Załaduj stronę w przeglądarce. Trzy WebPartZone strefy są widoczne ze swoimi zawartymi kontrolkami serwera. W dolnej części strony znajduje się kilka przycisków, z których każdy demonstruje jakiś aspekt pracy z obiektem WebPartZoneCollection . Kliknij każdy przycisk, aby zobaczyć jego wpływ na strefy i ich zawartość. Aby wyświetlić kod skojarzony z każdym przyciskiem, który pokazuje jakiś aspekt WebPartZoneCollection obiektu, wyświetl kod w pliku klasy częściowej, do którego odwołuje się powyższy kod jako druga część tego przykładu kodu.

Uwagi

Klasa WebPartZoneCollection zawiera kolekcję kontrolek, które istnieją na stronie składników WebPartZone Web Part. Jest on używany głównie przez kontrolkę WebPartManager do śledzenia wszystkich stref i zarządzania nimi. Kontrolka WebPartManager przechowuje odwołanie do kolekcji stref na stronie we właściwości tylko do Zones odczytu.

Klasa jest przydatna WebPartZoneCollection , gdy chcesz wykonywać operacje na wielu strefach lub ich zawartości w tym samym czasie, lub gdy trzeba zlokalizować określoną strefę i zbadać lub zmienić jego zawartość. Chociaż sama kolekcja stref jest tylko do odczytu i nie można zmienić rozmiaru obiektu kolekcji, nadal można uzyskać odwołania do poszczególnych stref w kolekcji, a jeśli masz odwołanie do pojedynczego obiektu strefy, możesz zmienić jego wartości właściwości.

Klasa WebPartZoneCollection ma dwa konstruktory. Pierwszy, , WebPartZoneCollection()po prostu inicjuje puste wystąpienie nowego WebPartZoneCollection. Druga , inicjuje kolekcję, WebPartZoneCollection(ICollection)przekazując istniejącą kolekcję stref.

Klasa WebPartZoneCollection zawiera Item[] właściwość. Ta właściwość umożliwia zlokalizowanie WebPartZone kontrolki w kolekcji przez przekazanie identyfikatora ciągu. Strefa znajduje się przez dopasowanie identyfikatora ciągu do wartości ID właściwości w WebPartZoneBase strefie.

Klasa WebPartZoneCollection ma również kilka metod, które są przydatne do badania lub uzyskiwania dostępu do jego zawartości. Metoda Contains określa, czy określona strefa istnieje w kolekcji. Metoda CopyTo kopiuje kolekcję do tablicy WebPartZoneBase obiektów. IndexOf Na koniec metoda zwraca indeks określonej strefy w kolekcji.

Konstruktory

WebPartZoneCollection()

Inicjuje puste wystąpienie WebPartZoneCollection klasy.

WebPartZoneCollection(ICollection)

Inicjuje WebPartZoneCollection wystąpienie klasy, przekazując kolekcję WebPartZone obiektów.

Właściwości

Count

Pobiera liczbę elementów zawartych w wystąpieniu ReadOnlyCollectionBase .

(Odziedziczone po ReadOnlyCollectionBase)
InnerList

Pobiera listę elementów zawartych w wystąpieniu ReadOnlyCollectionBase .

(Odziedziczone po ReadOnlyCollectionBase)
Item[Int32]

Zwraca określony element członkowski kolekcji, przekazując liczbę całkowitą.

Item[String]

Zwraca określony element członkowski kolekcji, przekazując unikatowy identyfikator.

Metody

Contains(WebPartZoneBase)

Zwraca wartość wskazującą, czy dana strefa istnieje w kolekcji.

CopyTo(WebPartZoneBase[], Int32)

Kopiuje kolekcję do tablicy WebPartZoneBase obiektów.

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetEnumerator()

Zwraca moduł wyliczający, który iteruje za pośrednictwem ReadOnlyCollectionBase wystąpienia.

(Odziedziczone po ReadOnlyCollectionBase)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera bieżące wystąpienie.

(Odziedziczone po Object)
IndexOf(WebPartZoneBase)

Zwraca pozycję określonego elementu członkowskiego kolekcji.

MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Jawne implementacje interfejsu

ICollection.CopyTo(Array, Int32)

Kopiuje całą ReadOnlyCollectionBase do zgodnej jednowymiarowej Arraytablicy, zaczynając od określonego indeksu tablicy docelowej.

(Odziedziczone po ReadOnlyCollectionBase)
ICollection.IsSynchronized

Pobiera wartość wskazującą, czy dostęp do ReadOnlyCollectionBase obiektu jest synchronizowany (bezpieczny wątek).

(Odziedziczone po ReadOnlyCollectionBase)
ICollection.SyncRoot

Pobiera obiekt, który może służyć do synchronizowania dostępu do ReadOnlyCollectionBase obiektu.

(Odziedziczone po ReadOnlyCollectionBase)

Metody rozszerzania

Cast<TResult>(IEnumerable)

Rzutuje elementy elementu IEnumerable na określony typ.

OfType<TResult>(IEnumerable)

Filtruje elementy elementu IEnumerable na podstawie określonego typu.

AsParallel(IEnumerable)

Umożliwia równoległość zapytania.

AsQueryable(IEnumerable)

Konwertuje element IEnumerable na .IQueryable

Dotyczy

Zobacz też