Transferring ASP.NET Form Results to 2nd Page

Since ASP.Net forms submit to themselves by default, many people have asked how to get the form results passed on to another page. There is more than one way to do this. One pretty much entails not putting the ‘runat=”server”‘ designation in the form tag, building it just like it was in Classic ASP. However, the main focus of this tutorial is showing you how to accomplish this using the Server.Transfer method.

Server.Transfer actually transfers the user to a new page, but there is one down side to this, in some people’s eyes. The fact that you are on another page may not be readily known by the end-user. The TITLE of the page changes, but the address bar of the receiving page remains the same as the first page.

So here’s the full code of what we need on the first page: Yes, Server.Transfer DOES technically work by itself because it moves the browser to the next page, but it takes a little extra work to be able to take all the form results with it when the system gets to the next page. Everything starts with the form, naturally. Here’s a sample form for the scenario.

<Form id="form1" runat="server">
	First Name:  <asp:TextBox id="txtFirst" runat="server" /><br>
	Last Name:  <asp:TextBox id="txtLast" runat="server" />
	<asp:Button id="button1" Text="Submit" onclick="doSubmit" runat="server" />
</Form>

As you can see, it’s basically two text boxes for User input – FirstName and LastName. The click event of the button goes to a subroutine that does all the work, as it would for any form action. Here we are going to come in contect with theĀ Context Collection. What needs to be done here, in preparation for sending the form results to the next page, is to add each form field item to theContext Collection

.

Context.Items.Add("first", txtFirst.text)
Context.Items.Add("last", txtLast.text)

The first parameter is the name we give to the item, which will also be referred to on the following page. Then, the second parameter is pointer to the exact form field property (in this case, the textbox TEXT property), to populate the first parameter.

To get to the next page, we merely use the following code, which can be copied and pasted in to a page on your computer:

Server.Transfer(“NextPage.aspx”)

So here’s the full code of what we need on the first page:

<script language="VB" runat="server">
Sub doSubmit(Source as Object, E as EventArgs)
	Context.Items.Add("first", txtFirst.text)
	Context.Items.Add("last", txtLast.text)
	Server.Transfer("Page2.aspx")
End Sub
</script>

<Form id="form1" runat="server">
	First Name:  <asp:TextBox id="txtFirst" runat="server" /><br>
	Last Name:  <asp:TextBox id="txtLast" runat="server" />
	<asp:Button id="button1" Text="Submit" onclick="doSubmit" runat="server" />
</Form>

Then, in the Page_Load event of the receiving page, we need to retrieve those context items. In Classic ASP, we would normally use the Request.form Method. Here’s how we ‘get’ those Context Items we added on the previous page:

Context.Items("first")
Context.Items("last")

So, for simplicity’s sake, we will display the results on the second page, by adding a label (label1) to the page, displaying and populating it with the context items from the previous page. Here’s the full code for Page2.aspx:

<html>
<head>
	<meta name="GENERATOR" Content="ASP Express 2.2">
	<title>Second Page</title>
<script language="VB" runat="server">
Sub Page_Load(Source as Object, E as EventArgs)
	label1.text = "The Name you entered was : " &  Context.Items("first") & " " & _
	Context.Items("last")
End Sub
</script>
<asp:Label ID="label1"  runat="server" />
</body>
</html>

That’s all there is to it. Now, you can create two pages (Page1.aspx and Page2.aspx) on your site and then copy and paste the code you see above to the appropriate pages and duplicate it locally. It’s that easy!

Related Posts:

  • No Related Posts
Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!