MultiView 和 View 控件和制作出选项卡的效果,MultiView 控件是一组 View 控件的容器。使用它可定义一组 View 控件,其中每个 View 控件都包含子控件。
如果要切换视图,可以使用控件的ID或者View控件的索引值。在 MultiView 控件中,一次只能将一个 View 控件定义为活动视图。如果某个 View 控件定义为活动视图,它所包含的子控件则会呈现到客户端。可以使用 ActiveViewIndex 属性或SetActiveView 方法定义活动视图。如果 ActiveViewIndex 属性为空,则 MultiView 控件不向客户端呈现任何内容。如果活动视图设置为MultiView 控件中不存在的 View,则会在运行时引发 ArgumentOutOfRangeException。
一些常用的属性、方法:
ActiveViewIndex属性:用于获取或设置当前被激活显示的View控件的索引值。默认值为-1,表示没有View控件被激活。
废话不多说,由于MultiView和View比较简单,我们一起来看以下例子吧。
创建新的 ASP.NET 网站项目
1.在“文件”菜单中,指向“新建”,然后选择“网站”。
2.在“新建网站”对话框中,从“语言”下拉列表中选择 Visual C#,并选择 ASP.NET 网站模板。
3.在“位置”中,选择 HTTP 并键入网站的 URL。默认的 URL 为 http://localhost/WebSite。改为http://localhost/MultiViewTest,单击“确定”。
4. 打开Default.aspx设计器,切换到代码区,Ctrl+A全选,替换为以下代码:
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <title>无标题页</title> 8 </head> 9 <body> 10 <form id="form1" runat="server"> 11 <div> 12 <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">first</asp:LinkButton> 13 <asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click">second</asp:LinkButton> 14 <asp:LinkButton ID="LinkButton3" runat="server" onclick="LinkButton3_Click">third</asp:LinkButton> 15 <br /> 16 <hr /> 17 <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex = 1> 18 <asp:View ID="View1" runat="server"> 19 this is the first page 20 </asp:View> 21 <asp:View ID="View2" runat="server"> 22 this is the second page 23 </asp:View> 24 <asp:View ID="View3" runat="server"> 25 this is the third page 26 </asp:View> 27 </asp:MultiView> 28 </div> 29 </form> 30 </body> 31 </html>