C# - Navigation using the Transfer method
Using the "Transfer" method is similar to executing a hyperlink or using the "Redirect" method, with one difference:
"Transfer" can retain some information from the source page across requests. Setting the "Transfer" method's
"preserverForm" argument to "True" makes the form's "QueryString", "ViewState", and event procedure information
available in the destination form.
To be able to read one Web form's ViewState from another, you must first set the "EnableViewStateMac" attribute
in the Web form's "Page" directive to "False". By default, ASP.NET hashes ViewState information, and setting this
attribute to "False" disables that hashing so that the information can be read on the subsequent Web form.
The following line shows how to disable hashing so that a page's ViewState can be used from another page:
1. Add this to first Web Form's HTTP code
v------------------------v
<%@ Page language="C#" EnableViewStateMac="false" Codebehind="RedirectNTransfer.aspx.cs" AutoEventWireup="false" Inherits=" MCSDWebAppsVB.Transfer2" %>
2. The following event procedure for an ImageButton control shows how information can be passed between forms with the "Transfer" method
// WebForm1.aspx
private void LinkButton1_Click(object sender, System.EventArgs e)
{
// Transfer to another form, retaining ViewState.
Server.Transfer("WebForm2.aspx", true);
}
WARNING: ASP.NET hashes ViewState information to prevent malicious users from manually changing the information passed back
to the server and thus somehow corrupting data stored there. Disabling this hashing decreases the security of your Web application
Use the "Request" object's "Form" method to retrieve the "ViewState" information from the source Web form. The following code
displays the values of two controls from the Transfer1.aspx Web form after the preceding "Transfer" method executes:
// WebForm2.aspx
private void Page_Load(object sender, System.EventArgs e)
{
System.Collections.Specialized.NameValueCollection colForm;
// Get data from the source Web Form
colForm = Request.Form;
// Display the value from Webform1's Txt_Text1's TextBox
txt_Add.Text = colForm["txt_Text1"];
}
NOTE: The "Server" object's "Transfer" and "Execute" methods work exclusively with Web forms. Trying to navigate to an
HTML page using one of these methods results in a run-time error