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.