五、ViewCollection 类
ViewCollection 类表示一个集合容器,该容器使 MultiView 控件可以维护其子控件列表。MultiView 控件只能包含 View 控件作为子控件。
使用 Add 方法可以将新的 View 控件添加到 ViewCollection 集合中的序号索引数组末尾。使用 AddAt 方法可以将新控件添加到特定的索引位置。使用 Item 索引器可以用简单的数组表示法,从 ViewCollection 集合中的指定索引位置获取 View 控件。
示例
下面的代码示例演示如何以编程方式将 View 控件添加到 MultiView 控件中。创建每个 View 控件后,使用 AddAt 方法将 View 控件添加到 MultiView 控件的 ViewCollection 集合中的指定索引位置。使用 Item 索引器访问存储在 ViewCollection 集合中的 View 控件的 ID 属性并向用户显示这些属性。
<%@ Page Language="VB"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<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>
六、View 类
ActiveViewIndex 属性指定 MultiView 控件的 Views 集合中的活动 View 控件。只要包含 MultiView 控件是可见的,活动视图控件就会呈现给客户端。使用 Visible 属性确定 View 控件及其子控件在页面上是否可见,以及是否呈现给客户端。
View 控件可以包含任何类型的控件,包括其他 MultiView 控件。View 控件不支持任何样式属性。若要对 View 控件应用样式,请将一个或多个 Panel 控件添加到 View 控件中。
View 类提供 Activate 和 Deactivate 事件。当前 View 控件成为活动视图时,引发 Activate 事件。这在 ActiveViewIndex 属性的值更改或调用 SetActiveView 方法以指定不同 View 控件时发生。例如,如果 View1 是 MultiView 控件中的活动视图,当 ActiveViewIndex 属性更改为指定 View2 时,则为 View2 引发 Activate 事件,为 View1 引发 Deactivate 事件。
若要允许用户在 MultiView 控件中的多个 View 控件之间进行定位,可将 LinkButton 或 Button 控件添加到每个 View 控件。将 LinkButton 或 Button 控件的 CommandName 属性设置为要定位到的 View 控件的 ID。
示例
下面的代码示例演示如何创建包含三个 View 控件的 MultiView 控件。第一次加载页面时,DefaultView 被设置为活动视图。每个 View 控件都包含链接按钮以便用户导航到其他视图。请注意,每个 View 控件都包含一个允许应用样式的 Panel 控件。
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head>
<title>MultiView Class Example</title>
<script runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' The first time the page loads,
' render the DefaultView.
If Not IsPostBack Then
' Set DefaultView as the active view.
MultiView1.SetActiveView(DefaultView)
End If
End Sub
Sub LinkButton_Command(sender As Object, e As System.Web.UI.WebControls.CommandEventArgs)
' Determine which link button was clicked
' and set the active view to
' the view selected by the user.
Select Case (e.CommandArgument)
Case "DefaultView"
MultiView1.SetActiveView(DefaultView)
Case "News"
MultiView1.SetActiveView(NewsView)
Case "Shopping"
MultiView1.SetActiveView(ShoppingView)
Case Else
Throw New Exception("You did not select a valid list item.")
End Select
End Sub
</script>
</head>
<body>
<form id="Form1" runat="server">
<h3>MultiView Class Example</h3>
<asp:MultiView id="MultiView1"
runat="Server">
<asp:View id="DefaultView"
runat="Server">
<asp:Panel id="DefaultViewPanel"
Width="330px"
BackColor="#C0C0FF"
BorderColor="#404040"
BorderStyle="Double"
runat="Server">
<asp:Label id="DefaultLabel1"
Font-bold="true"
Font-size="14"
Text="The Default View"
runat="Server"
AssociatedControlID="DefaultView">
</asp:Label>
<asp:BulletedList id="DefaultBulletedList1"
BulletStyle="Disc"
DisplayMode="Hyperlink"
Target="_blank"
runat="Server">
<asp:ListItem Value="http://www.microsoft.com">Today's Weather</asp:ListItem>
<asp:ListItem Value="http://www.microsoft.com">Today's Stock Quotes</asp:ListItem>
<asp:ListItem Value="http://www.microsoft.com">Today's News Headlines</asp:ListItem>
<asp:ListItem Value="http://www.microsoft.com">Today's Featured Shopping</asp:ListItem>
</asp:BulletedList>
<hr />
<asp:Label id="DefaultLabel2"
Font-size="12"
Text="Click a link to display a different view:"
runat="Server">
</asp:Label><br />
<asp:LinkButton id="Default_NewsLink"
Text="Go to News View"
OnCommand="LinkButton_Command"
CommandArgument="News"
CommandName="Link"
Width="150px"
runat="Server">
</asp:LinkButton>
<asp:LinkButton id="Default_ShoppingLink"
Text="Go to Shopping View"
OnCommand="LinkButton_Command"
CommandArgument="Shopping"
CommandName="Link"
Width="150px"
runat="server">
</asp:LinkButton><br /><br />
</asp:Panel>
</asp:View>
<asp:View id="NewsView"
runat="Server">
<asp:Panel id="NewsPanel1"
Width="330px"
BackColor="#C0FFC0"
BorderColor="#404040"
BorderStyle="Double"
runat="Server">
<asp:Label id="NewsLabel1"
Font-bold="true"
Font-size="14"
Text="The News View"
runat="Server"
AssociatedControlID="NewsView">
</asp:Label>
<asp:BulletedList id="NewsBulletedlist1"
BulletStyle="Disc"
DisplayMode="Hyperlink"
Target="_blank"
runat="Server">
<asp:ListItem Value="http://www.microsoft.com">Today's International Headlines</asp:ListItem>
<asp:ListItem Value="http://www.microsoft.com">Today's National Headlines</asp:ListItem>
<asp:ListItem Value="http://www.microsoft.com">Today's Local News</asp:ListItem>
</asp:BulletedList>
<hr />
<asp:Label id="NewsLabel2"
Font-size="12"
Text="Click a link to display a different view:"
runat="Server">
</asp:Label><br />
<asp:LinkButton id="News_DefaultLink"
Text="Go to the Default View"
OnCommand="LinkButton_Command"
CommandArgument="DefaultView"
CommandName="Link"
Width="150px"
runat="Server">
</asp:LinkButton>
<asp:LinkButton id="News_ShoppingLink"
Text="Go to Shopping View"
OnCommand="LinkButton_Command"
CommandArgument="Shopping"
CommandName="Link"
Width="150px"
runat="Server">
</asp:LinkButton><br /><br />
</asp:Panel>
</asp:View>
<asp:View id="ShoppingView"
runat="Server">
<asp:Panel id="ShoppingPanel1"
Width="330px"
BackColor="#FFFFC0"
BorderColor="#404040"
BorderStyle="Double"
runat="Server">
<asp:Label id="ShoppingLabel1"
Font-Bold="true"
Font-size="14"
Text="The Shopping View"
runat="Server"
AssociatedControlID="ShoppingView">
</asp:Label>
<asp:BulletedList id="ShoppingBulletedlist1"
BulletStyle="Disc"
DisplayMode="Hyperlink"
Target="_blank"
runat="Server">
<asp:ListItem Value="http://www.microsoft.com">Shop for Home and Garden </asp:ListItem>
<asp:ListItem Value="http://www.microsoft.com">Shop for Women's Fashions</asp:ListItem>
<asp:ListItem Value="http://www.microsoft.com">Shop for Men's Fashions</asp:ListItem>
</asp:BulletedList>
<hr />
<asp:Label id="ShoppingLabel2"
Font-size="12"
Text="Click a link to display a different view:"
runat="Server">
</asp:Label><br />
<asp:LinkButton id="Shopping_DefaultLink"
Text="Go to the Default View"
OnCommand="LinkButton_Command"
CommandArgument="DefaultView"
CommandName="Link"
Width="150px"
runat="Server">
</asp:LinkButton>
<asp:LinkButton id="Shopping_NewsLink"
Text="Go to News View"
OnCommand="LinkButton_Command"
CommandArgument="News"
CommandName="Link"
Width="150px"
runat="Server">
</asp:LinkButton><br /><br />
</asp:Panel>
</asp:View>
</asp:MultiView>
</form>
</body>
</html>
七、MultiViewControlBuilder 类
与分析器进行交互以生成 MultiView 控件。