- .NET Web高級(jí)開發(fā)
- 羅江華 朱永光編著
- 1505字
- 2018-12-29 13:19:41
1.2.2 MultiView控件
在B/S項(xiàng)目的開發(fā)中,有時(shí)可能要把一個(gè)Web頁面分成不同的塊,而每次只顯示其中一塊,同時(shí)又能方便地在塊與塊之間導(dǎo)航。這種技術(shù)常用于在一個(gè)靜態(tài)頁面中引導(dǎo)用戶完成多個(gè)步驟的操作,如會(huì)員注冊(cè)、在線調(diào)查功能的實(shí)現(xiàn)。盡管已經(jīng)有一個(gè)專門為此目的設(shè)計(jì)的Wizard控件,但是仍然可能使用MultiView和View控件來創(chuàng)建類似于向?qū)У膽?yīng)用程序。塊是頁面中某區(qū)域的內(nèi)容,ASP.NET提供了View控件對(duì)塊進(jìn)行管理。每個(gè)塊對(duì)應(yīng)一個(gè)View控件,所有View對(duì)象包含在MultiView對(duì)象中。MultiView中每次只顯示一個(gè)View對(duì)象。這個(gè)對(duì)象稱為活動(dòng)視圖。
MultiView控件有一個(gè)類型為ViewCollection的只讀屬性View。使用該屬性可獲得包含在MultiView中的View對(duì)象集合。與所有的.NET集合一樣,該集合中的元素被編入索引。MultiView控件包含ActiveViewIndex屬性,該屬性可獲取或設(shè)置以“0”開始的當(dāng)前活動(dòng)視圖的索引。如果沒有視圖是活動(dòng)的,那么ActiveViewIndex為默認(rèn)值“-1”。表1-3給出MultiView控件的4個(gè)CommandName字段,為按鈕的CommandName屬性賦值,能夠?qū)崿F(xiàn)視圖導(dǎo)航。
表1-3 MultiView控件的CommandName字段

將某些控件如ImageButton的CommandName屬性設(shè)置為NextView,單擊這些按鈕后將自動(dòng)導(dǎo)航到下一個(gè)視圖,而不需要額外的代碼。開發(fā)者不需要為按鈕編寫單擊事件處理程序。通過調(diào)用MultiView控件的SetActiveView或GetActiveView方法也可以設(shè)置或獲取活動(dòng)視圖。SetActiveView使用View對(duì)象作為參數(shù),而GetActiveView則返回一個(gè)View對(duì)象。每次視圖發(fā)生變化時(shí),頁面都會(huì)被提交到服務(wù)器,同時(shí)MultiView控件和View控件將觸發(fā)多個(gè)事件。活動(dòng)視圖發(fā)生變化時(shí),MultiView控件將觸發(fā)ActiveViewChanged事件。與此同時(shí),新的活動(dòng)視圖將觸發(fā)Activate事件,原活動(dòng)視圖則觸發(fā)Deactivate事件。所有的事件都包含一個(gè)EventArgs類型的參數(shù)。該參數(shù)只是一個(gè)占位符,它沒有提供與事件相關(guān)的附加信息。然而,與所有的事件處理程序一樣,對(duì)事件源的引用將傳遞給事件處理程序。View控件包含一個(gè)Boolean類型的Visible屬性,設(shè)置該屬性可以控制特定View對(duì)象的可見性,或以編程方式確定哪一個(gè)View是可見的。MultiView和View控件都沒有樣式屬性。對(duì)于MultiView控件而言,這不足為奇。畢竟它只不過是View控件的容器而已。對(duì)于View控件而言,如果要使用樣式屬性,則必須將樣式應(yīng)用到每一個(gè)它包含的控件中。還有一種方法是在View控件中嵌入一個(gè)Panel控件,并設(shè)置Panel的樣式屬性。
下面的示例演示如何使用MultiView控件來創(chuàng)建基本調(diào)查。每個(gè)View控件都是一個(gè)單獨(dú)的調(diào)查問題。用戶單擊任何頁上的“上一頁”按鈕時(shí),將減小ActiveViewIndex屬性的值,以定位到上一個(gè)View控件。用戶單擊任何頁上的“下一頁”按鈕時(shí),將增大ActiveViewIndex屬性的值,以定位到下一個(gè)View控件:
<h3>MultiView ActiveViewIndex Example</h3> <asp:Panel id="Page1ViewPanel" Width="330px" Height="150px" HorizontalAlign =Left Font-size="12" BackColor="#C0C0FF" BorderColor="#404040" BorderStyle="Double" runat="Server"> <asp:MultiView id="DevPollMultiView" ActiveViewIndex=0 runat="Server"> <asp:View id="Page1" runat="Server"> <asp:Label id="Page1Label" Font-bold="true" Text="What kind of applications do you develop?" runat="Server"> </asp:Label><br><br> <asp:RadioButton id="Page1Radio1" Text="Web Applications" Checked="False" GroupName="RadioGroup1" runat="server" > </asp:RadioButton><br> <asp:RadioButton id="Page1Radio2" Text="Windows Forms Applications" Checked="False" GroupName="RadioGroup1" runat="server" > </asp:RadioButton><br> <asp:Button id="Page1Next" Text = "Next" OnClick="NextButton_Command" Height="25" Width="70" runat= "Server"> </asp:Button> </asp:View> <asp:View id="Page2" runat="Server"> <asp:Label id="Page2Label" Font-bold="true" Text="How long have you been a developer?" runat="Server"> </asp:Label><br><br> <asp:RadioButton id="Page2Radio1" Text="Less than five years" Checked="False" GroupName="RadioGroup1" runat="Server"> </asp:RadioButton><br> <asp:RadioButton id="Page2Radio2" Text="More than five years" Checked="False" GroupName="RadioGroup1" runat="Server"> </asp:RadioButton><br> <asp:Button id="Page2Back" Text = "Previous" OnClick="BackButton_Command" Height="25" Width="70" runat= "Server"> </asp:Button> <asp:Button id="Page2Next" Text = "Next" OnClick="NextButton_Command" Height="25" Width="70" runat="Server"> </asp:Button> </asp:View> <asp:View id="Page3" runat="Server"> <asp:Label id="Page3Label1" Font-bold="true" Text= "What is your primary programming language?" runat="Server"> </asp:Label><br><br> <asp:RadioButton id="Page3Radio1" Text="Visual Basic .NET" Checked="False" GroupName="RadioGroup1" runat="Server"> </asp:RadioButton><br> <asp:RadioButton id="Page3Radio2" Text="C#" Checked="False" GroupName="RadioGroup1" runat="Server"> </asp:RadioButton><br> <asp:RadioButton id="Page3Radio3" Text="C++" Checked="False" GroupName="RadioGroup1" runat="Server"> </asp:RadioButton><br> <asp:Button id="Page3Back" Text = "Previous" OnClick="BackButton_Command" Height="25" Width="70" runat="Server"> </asp:Button> <asp:Button id="Page3Next" Text = "Next" OnClick="NextButton_Command" Height="25" Width="70" runat="Server"> </asp:Button><br> </asp:View> <asp:View id="Page4" runat="Server"> <asp:Label id="Label1" Font-bold="true" Text = "Thank you for taking the survey." runat="Server"> </asp:Label> <br><br> <asp:Button id="Page4Save" Text = "Save Responses" OnClick="NextButton_Command" Height="25" Width="110" runat="Server"> </asp:Button> <asp:Button id="Page4Restart" Text = "Retake Survey" OnClick="BackButton_Command" Height="25" Width="110" runat= "Server"> </asp:Button> </asp:View> </asp:MultiView> </asp:Panel>
后臺(tái)代碼如下:
protected void NextButton_Command(object sender, EventArgs e) { if (DevPollMultiView.ActiveViewIndex > -1 & DevPollMultiView.ActiveView -Index < 3) { DevPollMultiView.ActiveViewIndex += 1; } else if (DevPollMultiView.ActiveViewIndex == 3) { Page4Save.Enabled = false; Page4Restart.Enabled = false; } else { throw new Exception("An error occurred."); } } protected void BackButton_Command(object sender, EventArgs e) { if (DevPollMultiView.ActiveViewIndex > 0 & DevPollMultiView.ActiveView -Index <= 2) { DevPollMultiView.ActiveViewIndex -= 1; } else if (DevPollMultiView.ActiveViewIndex == 3) { DevPollMultiView.ActiveViewIndex = 0; } else { throw new Exception("An error occurred."); } }
實(shí)現(xiàn)效果如圖1-10和圖1-11所示。

圖1-10

圖1-11
總地來說,MultiView就像<Table></Table>和<TD></TD>一樣,View要放在MultiView中,它們兩個(gè)才能起作用。可以把它們看成一個(gè)Panel集合,但是表現(xiàn)為“單選”狀態(tài),即當(dāng)前只顯示一個(gè),用起來比Panel要省事得多。
- 精通MATLAB神經(jīng)網(wǎng)絡(luò)
- Hands-On Artificial Intelligence on Amazon Web Services
- AWS:Security Best Practices on AWS
- 數(shù)據(jù)運(yùn)營(yíng)之路:掘金數(shù)據(jù)化時(shí)代
- 工業(yè)機(jī)器人工程應(yīng)用虛擬仿真教程:MotoSim EG-VRC
- 最后一個(gè)人類
- Maya 2012從入門到精通
- Practical Big Data Analytics
- 嵌入式操作系統(tǒng)原理及應(yīng)用
- 單片機(jī)原理實(shí)用教程
- 經(jīng)典Java EE企業(yè)應(yīng)用實(shí)戰(zhàn)
- 筆記本電腦維修之電路分析基礎(chǔ)
- 穿越計(jì)算機(jī)的迷霧
- Cloudera Hadoop大數(shù)據(jù)平臺(tái)實(shí)戰(zhàn)指南
- Raspberry Pi Projects for Kids