3.3. Submitting a Form to a Different Page

Problem

You need to submit the information on one page—a form, for example—to another. You might want to do this in order to use one page to collect form data and a second page to process it.

Tip

The ASP.NET model does not “normally” support submitting a form to any page other than itself. If you are willing to revert to the classic ASP model by removing the runat="server" attribute from the form element, the page can be submitted to any other page in the same manner, as was done in ASP pages. We don’t recommend this approach, however, because it does not take advantage of the built-in functionality provided by the server controls to automatically recreate the controls and populate them with the data submitted with the form, making your coding tasks much harder.

Solution

There are two ways to solve this problem:

  • Use the Server.Transfer method in a button click event handler in the code-behind of the first page to transfer control to a second page, after saving the contents of the first in session scope. Example 3-7 through Example 3-12 show the .aspx and code-behind files for the application that implements this solution.

  • Use the Server.Transfer method in a button click event handler in the code-behind of the first page to transfer control, along with the form contents in the viewstate, to the second page. This approach is described at the end of Recipe 3.2.3 and includes some give-and-take about why one approach might be favored over the other, ...

Get ASP.NET Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.