Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

ViewCollection Class

Represents a collection container that enables a MultiView control to maintain a list of its child controls.

System.Object
  System.Web.UI.ControlCollection
    System.Web.UI.WebControls.ViewCollection

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)
public class ViewCollection : ControlCollection

The ViewCollection type exposes the following members.

  NameDescription
Public methodViewCollectionInitializes a new instance of the ViewCollection class.
Top
  NameDescription
Public propertyCountGets the number of server controls in the ControlCollection object for the specified ASP.NET server control. (Inherited from ControlCollection.)
Public propertyIsReadOnlyGets a value indicating whether the ControlCollection object is read-only. (Inherited from ControlCollection.)
Public propertyIsSynchronizedGets a value indicating whether the ControlCollection object is synchronized. (Inherited from ControlCollection.)
Public propertyItemGets a reference to the View control at the specified index location in the ViewCollection collection.
Protected propertyOwnerGets the ASP.NET server control to which the ControlCollection object belongs. (Inherited from ControlCollection.)
Public propertySyncRootGets an object that can be used to synchronize access to the collection of controls. (Inherited from ControlCollection.)
Top
  NameDescription
Public methodAddAdds the specified View control to the collection. (Overrides ControlCollection.Add(Control).)
Public methodAddAtAdds the specified View control to the collection at the specified index location. (Overrides ControlCollection.AddAt(Int32, Control).)
Public methodClearRemoves all controls from the current server control's ControlCollection object. (Inherited from ControlCollection.)
Public methodContainsDetermines whether the specified server control is in the parent server control's ControlCollection object. (Inherited from ControlCollection.)
Public methodCopyToCopies the child controls stored in the ControlCollection object to an System.Array object, beginning at the specified index location in the System.Array. (Inherited from ControlCollection.)
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetEnumeratorRetrieves an enumerator that can iterate through the ControlCollection object. (Inherited from ControlCollection.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIndexOfRetrieves the index of a specified Control object in the collection. (Inherited from ControlCollection.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodRemoveRemoves the specified server control from the parent server control's ControlCollection object. (Inherited from ControlCollection.)
Public methodRemoveAtRemoves a child control, at the specified index location, from the ControlCollection object. (Inherited from ControlCollection.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The ViewCollection class represents a collection container that enables a MultiView control to maintain a list of its child controls. A MultiView control can contain only View controls as child controls.

Use the Add method to add a new View control to the ViewCollection collection at the end of an ordinal index array. Use the AddAt method to add a new control at a specific index location. Use the Item indexer to get a View control from the ViewCollection collection at a specified index, using simple array notation.

The following code example demonstrates how to programmatically add View controls to a MultiView control. After each View control is created, the AddAt method is used to add the View control to the ViewCollection collection of the MultiView control at the specified index. The Item indexer is used to access the ID properties of the View controls stored in the ViewCollection collection and display them to the user.

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

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.