Transfer and Execute Methods in ASP.NET...
One of the biggest complaints from developers moving from ASP to .NET is the inability to post data to another form. This is only partially true. The HTTPServerUtility class's Transfer and Execute methods handle this to an extent.
When talking to ASP developers about some of the changes that ASP.NET brings, one of the biggest complaints is the inability to post data to another form. This statement is only partially true; forms can't post back through the action property anymore. However, the HTTPServerUtility class's Transfer and Execute methods handle this functionality to an extent.
Let's first look at each method in example. When transferring data from the first page to the second page, the form values are available to the second page through the current context’s HTTP handler, provided the server control or property that is to be referenced is declared public. The execute method works in a very similar way; the content is transferred through the current context’s HTTP handler, which is available to the second page. The difference between the two methods is that executing a page will insert the second page’s content into the first page, whereas transferring redirects to the second page, while still referencing the first page in the browser's address bar.
'Simple statement in Page1 event (such as a link button click event)
Server.Transfer("transfer2.aspx", True)
OR
Server.Execute("execute2.aspx")
'Code in Page2’s Page_Load event
Dim objForm As Page1 = CType(HttpContext.Current.Handler, Page1)
'transfer1's textbox controls are declared public
TextBox1.Text = objForm.TextBox1.Text
TextBox2.Text = objForm.TextBox2.Text
When dealing with the Transfer method, it may cause problems in an application's navigation scheme. Although the attached code example doesn't have that problem, I've experienced that issue when dealing with more complex applications that involve a lot more page redirections.
When using the Transfer method, the second Boolean parameter allows the transfer to not transmit the form data from the source form to the destination form. In addition, the Execute method has an overloaded method that uses a TextWriter object to write the HTML contents to. For example, using the Page1/Page2 example, Page1 passes a TextWriter object in the Execute method. The contents are written to this object, which is then written in the context of Page1.
'Code in one of Page1's events (such as a link button click event);
'The TextWriter can then render the contents at any point in the page
'execution
Dim objWriter As System.IO.TextWriter
Server.Execute("execute4.aspx", objWriter)
Response.Write(objWriter.ToString())
'Code in Page2's Page_Load event
Dim objForm As execute3 = CType(HttpContext.Current.Handler, execute3)
Label1.Text = objForm.TextBox1.Text
Label2.Text = objForm.TextBox2.Text
This article is short and sweet, but is meant to show you other options for the lack of form-to-form posting in ASP.NET.
About the Code
The code attached is an ASP.NET project. Please copy the folder to your wwwroot directory in order to use this example. Additionally, you may have to set this project up as an application for the virtual directory. In IIS, right-click the Caching virtual directory, select Properties. Where it says "Application Name", the button next to it should say "Remove". If it doesn't, hit the create button to create the application.
You may download the code here.