ASP.NET揭秘读书笔记6

78 篇文章 0 订阅
 
Simulating Multipage Forms

Imagine that you have a form, which contains 50 questions that you want to break up into multiple pages. One way to do so would be to actually create a separate ASP.NET page for each group of questions.

Creating a form that spans multiple ASP.NET pages, however, would take a lot of work. You would need some method of storing the answers that a user entered for page 1 when the user is on page 5. You could use hidden form fields or Session variables, but there is an easier way.

Instead of breaking the form into multiple ASP.NET pages, you can place all the questions within a single ASP.NET page. You can use the Panel control to display and hide different sections of the form at a time.

The advantage of this approach is that all the answers entered into the form are automatically preserved. When the Panel control displays the questions for page 5, the answers to the questions for page 1 are still safely tucked away in an invisible panel.

The page in Listing 4.7 demonstrates how you would use this method to create a simulated three-page form.

Listing 4.7 PanelMultiPage.aspx
<Script Runat="Server">

Sub Page_Load
  If Not IsPostBack Then
    ViewState( "CurrentPage" ) = 1
  End If
End Sub

Sub btnNextPage_Click( s As Object, e As EventArgs )
  Dim pnlPanel As Panel
  Dim strPanelName AS String

  ' Hide Previous Panel
  strPanelName = "pnlForm" & ViewState( "CurrentPage" )
  pnlPanel = FindControl( strPanelName )
  pnlPanel.Visible = False

  ' Show Current Panel
  ViewState( "CurrentPage" ) += 1
  strPanelName = "pnlForm" & ViewState( "CurrentPage" )
  pnlPanel = FindControl( strPanelName )
  pnlPanel.Visible = True
End Sub

Sub btnPrevPage_Click( s As Object, e As EventArgs )
  Dim pnlPanel As Panel
  Dim strPanelName AS String

  ' Hide Current Panel
  strPanelName = "pnlForm" & ViewState( "CurrentPage" )
  pnlPanel = FindControl( strPanelName )
  pnlPanel.Visible = False

  ' Show Previous Panel
  ViewState( "CurrentPage" ) -= 1
  strPanelName = "pnlForm" & ViewState( "CurrentPage" )
  pnlPanel = FindControl( strPanelName )
  pnlPanel.Visible = True
End Sub

Sub btnFinish_Click( s As Object, e As EventArgs )
  pnlForm3.Visible = False
  pnlForm4.Visible = True
  lblSummary.Text = "<h2>You entered:</h2>"
  lblSummary.Text &= "<li> First Name=" & txtFirstname.Text
  lblSummary.Text &= "<li> Last Name=" & txtLastname.Text
  lblSummary.Text &= "<li> Color=" & txtFavColor.Text
  lblSummary.Text &= "<li> Philosopher=" & radlFavPhilosopher.SelectedItem.Text
End Sub

</Script>

<html>
<head><title>PanelMultiPage.aspx</title></head>
<body>

<form Runat="Server">

<asp:Panel ID="pnlForm1" Runat="Server">

<h3>Page <%=ViewState( "CurrentPage" )%> of 3</h3>
<hr>
First Name:
<asp:TextBox
  ID="txtFirstname"
  Runat="Server" />
<p>
Last Name:
<asp:TextBox
  ID="txtLastname"
  Runat="Server" />
<hr>
<asp:Button
  Text="Next Page >>"
  OnClick="btnNextPage_Click"
  Runat="Server" />

</asp:Panel>
<asp:Panel ID="pnlForm2" Runat="Server" Visible="False">

<h3>Page <%=ViewState( "CurrentPage" )%> of 3</h3>
<hr>
Favorite Color:
<asp:TextBox
  ID="txtfavColor"
  Runat="Server" />
<hr>
<asp:Button
  Text="<< Prev Page"
  OnClick="btnPrevPage_Click"
  Runat="Server" />
<asp:Button
  Text="Next Page >>"
  OnClick="btnNextPage_Click"
  Runat="Server" />

</asp:Panel>
<asp:Panel ID="pnlForm3" Runat="Server" Visible="False">

<h3>Page <%=ViewState( "CurrentPage" )%> of 3</h3>
<hr>
Favorite Philosopher:
<asp:RadioButtonList
  ID="radlFavPhilosopher"
  Runat="Server">
  <asp:ListItem Text="Frege" Selected="True"/>
  <asp:ListItem Text="Russell" />
  <asp:ListItem Text="Carnap" />
</asp:RadioButtonList>
<hr>
<asp:Button
  Text="<< Prev Page"
  OnClick="btnPrevPage_Click"
  Runat="Server" />
<asp:Button
  Text="Finish"
  OnClick="btnFinish_Click"
  Runat="Server" />

</asp:Panel>
<asp:Panel ID="pnlForm4" Runat="Server" Visible="False">

<asp:Label
  ID="lblSummary"
  Runat="Server"/>

</asp:Panel>

</form>
</body>
</html>

The C# version of this code can be found on the CD-ROM.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值