ViewCollection 類別

定義

表示集合容器 (Container),可讓 MultiView 控制項維護其子控制項的清單。

public ref class ViewCollection : System::Web::UI::ControlCollection
public class ViewCollection : System.Web.UI.ControlCollection
type ViewCollection = class
    inherit ControlCollection
Public Class ViewCollection
Inherits ControlCollection
繼承
ViewCollection

範例

下列程式碼範例示範如何以程式設計方式將控制項新增 ViewMultiView 控制項。 建立每個 View 控制項之後, AddAt 方法會用來將 View 控制項加入至 ViewCollection 指定索引處控制項 MultiView 的集合。 索引 Item[] 器可用來存取 ID 儲存在集合中的 ViewCollection 控制項屬性 View ,並將其顯示給使用者。

<%@ Page Language="VB"%>
<!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>
    <title>ViewCollection example</title>
<script runat="server">
      
        Sub Button1_Click(ByVal Sender As Object, ByVal e As EventArgs)
            ' Create a MultiView control.
            Dim MultiView1 As New MultiView

            ' Create a ViewCollection for the View 
            ' controls contained in MultiView1.
            Dim myViewCollection As New ViewCollection(MultiView1)

            ' Create a View control. 
            Dim View1 As New View
            ' Use a helper function to create the view.
            View1 = CreateView("View1")
            ' Add View1 to myViewCollection at index 0.
            myViewCollection.AddAt(0, View1)

            ' Create a second View control and 
            ' add it to myViewCollection at index 1.
            Dim View2 As New View
            View2 = CreateView("View2")
            myViewCollection.AddAt(1, View2)

            ' Create a third View control and 
            ' add it to myViewCollection at index 0.
            ' Inserting View3 at index 0 
            ' causes View1 to move to index 1  
            ' and View2 to move to index 2.
            Dim View3 As New View
            View3 = CreateView("View3")
            myViewCollection.AddAt(0, View3)

            ' Show the contents of myViewCollection on the page.
            DisplayViewCollectionContents(myViewCollection)
            
        End Sub

        ' A function to programmatically create a View control.
        Private Function CreateView(ByVal viewId As String) As View
            ' Create a View control
            Dim myView As New View
            myView.ID = viewId

            ' Create a Panel control.
            Dim Panel1 As New Panel

            ' Set the style properties for Panel1.
            Panel1.Height = New Unit(150)
            Panel1.Width = New Unit(150)
            Panel1.BackColor = System.Drawing.Color.Azure
            Panel1.BorderStyle = BorderStyle.Double

            ' Add Panel1 to the Controls collection
            ' of the View control.
            myView.Controls.Add(Panel1)

            ' Create a Label control.
            Dim Label1 As New Label

            ' Set the properties for Label1.
            Label1.Text = "This is " + CStr(myView.ID)

            ' Add Label1 to the Controls collection
            ' of the Panel1 control.
            Panel1.Controls.Add(Label1)

            Return myView
        End Function

        ' A sub-routine to display the contents of myViewCollection.
        Sub DisplayViewCollectionContents(ByVal collection As ViewCollection)
            ' Use the Item property to access the ID of the View
            ' control at the specified index in the collection.
            Label1.Text = "The view at index 0 is " + collection.Item(0).ID
            Label2.Text = "The view at index 1 is " + collection.Item(1).ID
            Label3.Text = "The view at index 2 is " + collection.Item(2).ID
        End Sub

</script>
 
</head>
<body>

    <form id="Form1" runat="server">

        <h3>ViewCollection example</h3> 

        <asp:Button id="Button2" 
            Text="Show ViewCollection contents" 
            OnClick="Button1_Click" 
            runat="Server"/>
        <br /><br />  
        
        <hr />
  
        <asp:Label ID="Label1"
            runat="Server">
        </asp:Label><br /><br /> 

        <asp:Label ID="Label2"
            runat="Server">
        </asp:Label><br /><br />

        <asp:Label ID="Label3"
            runat="Server">
        </asp:Label><br /><br /> 
       
    </form>
   
</body>
</html>

備註

類別 ViewCollection 代表集合容器,可讓 MultiView 控制項維護其子控制項的清單。 控制項 MultiView 只能 View 包含控制項做為子控制項。

Add使用 方法,將新的 View 控制項 ViewCollection 加入至序數索引陣列結尾的集合。 AddAt使用 方法,在特定的索引位置加入新的控制項。 Item[]使用索引子,透過簡單的陣列標記法,從集合中取得 View 集合中的 ViewCollection 控制項。

建構函式

ViewCollection(Control)

初始化 ViewCollection 類別的新執行個體。

屬性

Count

取得指定 ASP.NET 伺服器控制項的 ControlCollection 物件中的伺服器控制項數目。

(繼承來源 ControlCollection)
IsReadOnly

取得值,這個值表示 ControlCollection 物件是否唯讀。

(繼承來源 ControlCollection)
IsSynchronized

取得值,該值指出是否同步處理 ControlCollection 物件。

(繼承來源 ControlCollection)
Item[Int32]

取得位於 View 集合中指定索引位置之 ViewCollection 控制項的參考。

Owner

取得 ControlCollection 物件所屬的 ASP.NET 伺服器控制項。

(繼承來源 ControlCollection)
SyncRoot

取得可用來對控制項集合進行同步 (Synchronize) 存取的物件。

(繼承來源 ControlCollection)

方法

Add(Control)

將指定的 View 控制項加入集合。

AddAt(Int32, Control)

將指定的 View 控制項加入集合的指定索引位置。

Clear()

從目前伺服器控制項的 ControlCollection 物件中移除所有控制項。

(繼承來源 ControlCollection)
Contains(Control)

判斷指定伺服器控制項是否在伺服器控制項的 ControlCollection 物件之中。

(繼承來源 ControlCollection)
CopyTo(Array, Int32)

ControlCollection 中的指定索引位置開始,複製儲存於 Array 物件的子控制項至 Array 物件。

(繼承來源 ControlCollection)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

擷取可逐一查看 ControlCollection 物件的列舉值。

(繼承來源 ControlCollection)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IndexOf(Control)

擷取集合中指定 Control 物件的索引。

(繼承來源 ControlCollection)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Remove(Control)

從父伺服器控制項的 ControlCollection 物件移除指定伺服器控制項。

(繼承來源 ControlCollection)
RemoveAt(Int32)

在指定索引位置,從 ControlCollection 物件移除子控制項。

(繼承來源 ControlCollection)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

另請參閱