by . .
Feedback
|
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days):
30119/
51
|
|
|
Introduction |
Multi-Page Forms
Published 1/25/02 - For
ASP.NET
Introduction
In ASP.NET and Web Forms,
something was made a bit harder - Multi-Page Forms. ie. You couldn't submit
your form to another page, it always posted back to the same page. This
article is going to show you several work-arounds to this problem.
|
Method 1 - Client-side forms |
1) Remove runat="server"
Ok, if you remove runat="server"
on your form tags, the problem will go away. However, if you have more
advanced server controls then the problem won't go away, you will get errors
because without runat="server" on the form tag - it won't generate the
postback code.
|
Method 2 - Panels |
2) Panels
ASP.NET also introduced panels, these panels are like different areas of the page and you can turn the on and off, or add controls to them. When making multi-page forms you can use panels to show different pages of the form, although it is still on the same page.
<script language="VB" runat="server"> Sub page_load(sender as object, e as EventArgs)
End Sub
Sub Do_page1(sender as object, e as EventArgs) page1.Visible = false page2.Visible = true End Sub Sub Do_Page2(sender as object, e as EventArgs) page2.Visible = false page3.Visible = true End Sub </script> <form runat="server"> <asp:panel runat="Server" id="page1"> Enter your name: <asp:textbox id="name" runat="server" /><br> <asp:button text="Next Page" runat="server" OnClick="Do_page1" /> </asp:panel> <asp:panel runat="server" id="page2" visible="false"> What city do you live in? <asp:textbox id="city" runat="server" /><br> <asp:button text="Finish" runat="server" OnClick="Do_page2" /> </asp:panel> <asp:panel runat="server" id="page3" visible="false" > Thank you for completing the form! </asp:panel> </form> |
Live Demo |
See the Visible=False is set on the other pages and is changed on the Click event handlers. If your wondering what happens to the form values when they're submitted then just check the Viewstate on the last page, it's all there. |
Method 3 - Session Variables |
3) Session Variables
When the user submits the form you can store the values in session variables and then load them up on the next page.
<script language="VB" runat="server"> Sub Do_page1(sender as object, e as EventArgs) Session.Contents("name") = name.Text Response.Redirect("mpf3.aspx") End Sub </script> <form runat="server"> Enter your name: <asp:textbox id="name" runat="server" /><br> <asp:button text="Next Page" runat="server" OnClick="Do_page1" /> </form> |
Page 1 -Live Demo |
<script language="VB" runat="server"> Sub page_load(sender as object, e as EventArgs) Dim name = Session.Contents("name") End Sub
Sub Do_page2(sender as object, e as EventArgs) Session.Contents("city") = city.Text Response.Redirect("mpf4.aspx") End Sub </script> <form runat="server"> What city do you live in <%=name%>? <asp:textbox id="city" runat="server" /><br> <asp:button text="Next Page" runat="server" OnClick="Do_page2" /><p> </form> |
Page 2 -Live Demo |
Thanks <%=Session.Contents("name")%>, I hope it's nice in <%=Session.Contents("city")%>. |
Page 3 -Live Demo | |
Method 4 - Querystrings |
4) Generated QueryString
Well, if you don't like panels and don't like Session Variables then your running out of options - fast. But there is one last way that you may want to try - the good old Querystring.
<script language="VB" runat="server"> Sub Do_page1(sender as object, e as EventArgs) Response.Redirect("mpf6.aspx?name=" & name.Text) End Sub </script> <form runat="server"> Enter your name: <asp:textbox id="name" runat="server" /><br> <asp:button text="Next Page" runat="server" OnClick="Do_page1" /> </form> |
Page 1 -Live Demo |
<script language="VB" runat="server"> Sub Do_page2(sender as object, e as EventArgs) Response.Redirect("mpf7.aspx?name=" & Request.QueryString("name") & "&city=" & city.Text) End Sub </script> <form runat="server"> What city do you live in <%=Request.QueryString("name")%>? <asp:textbox id="city" runat="server" /><br> <asp:button text="Next Page" runat="server" OnClick="Do_page2" /><p> </form> |
Page 1 -Live Demo |
Thanks <%=Request.QueryString("name")%>, I hope it's nice in <%=Request.QueryString("city")%>. |
Page 1 -Live Demo |
This is not really an option if you have a large form because you'll have to build a huge querystring from scratch. Also, updating this kind of form is very hard (mainly with editing the Querystring). |
Method 5 - Context |
5) Using the Context object
(06/23/02)
This idea was submitted to me by Kirk Jackson, Thanks!
The context object is a variable that is initalized at the start of each request and will last until the end of the request. You can store items in this object and then transfer the page using Server.Transfer.
Page 1 -
<script language="VB" runat="server"> Sub Do_page1(sender as object, e as EventArgs) Context.Items("Name") = name.Text Server.Transfer("mpf9.aspx") End Sub </script> <form runat="server"> Enter your name: <asp:textbox id="name" runat="server" /><br> <asp:button text="Next Page" runat="server" OnClick="Do_page1" /> </form> |
This would store it and sent it along.
Last Page
Thanks <%=Context.Items("Name")%>, I hope it's nice in <%=Context.Items("City")%>. |
Summary
Well, you have learnt the ways to work-around that annoying 'Always-post-back' problem in ASP.NET |
|
|
|
Product Spotlight
|
|