<%@ Page %>
Using Server.Transfer and EnableViewStateMac to Preserve Form State
Introduction
In classic ASP all of you must have posted one form to another by setting the action attribute. However, this doesn't work the same with ASP.NET. The web form model requires that each form be posted to the same form. If you want to transfer control from one web form to another you need to use Server.Transfer() method. Refer my
previous article that examines several ways of passing values from one web form to another. Many ASP.NET programmers have misconception that the Server.Transfer() method can not be used to pass form state to another form. However, with some tweaks you can do so easily.
Server.Transfer Method
Server.Transfer method is used to transfer control from one web form to another. Unlike Response.Redirect - which actually asks browser to make a new request again - Server.Transfer transfers the control on server side itself. The Server.Transfer method takes two parameters. The web form to which the control will be transferred and a boolean value indicating whether the current form state will be preserved in the new form or not. Let us build an application that first illustrates the problem while using this method and then see how to overcome the problem.
Problem with Server.Transfer
Create a new ASP.NET web application and add two web forms (.aspx) to it. Let us call the forms as Page1.aspx and Page2.aspx. In Page1.aspx place one textbox with ID TextBox1 and one Button with ID Button1. In the Click event of the button write following code:
Server.Transfer("Page2.aspx", True)
Now, run the application by navigating to Page1.aspx. Enter some value in the TextBox and click the button. You should get an error stating that "The View State is invalid for this page and might be corrupted". This happens because of an attribute of @Page directive called EnableViewStateMac of the second page (Page2.aspx). By default this attribute is set to True by ASP.NET and that causes the error.
What is EnableViewStateMac?
Here is what MSDN documentation has to say about EnableViewStateMac:
EnableViewStateMac
Indicates that ASP.NET should run a machine authentication
check (MAC) on the page's view state when the page is
posted back from the client. true if view state should
be MAC checked; otherwise, false. The default is false.
Note A view state MAC is an encrypted version the hidden
variable that a page's view state is persisted to when sent
to the browser. When you set this attribute to true, the
encrypted view state is checked to verify that it has not
been tampered with on the client.
Believe me or not the default value of this attribute is True and not False as mentioned in the MSDN documentation.
Solution
In order to overcome the problem of ViewState mentioned above you need to explicitly set EnableViewStateMac attribute to False for the destination page i.e. Page2.aspx in our case.
Page2.aspx
----------
<%@ Page Language="vb" EnableViewStateMac="False"%>
Once you do that you can access value entered in TextBox1 of Page1.aspx in Page2.aspx without any error.
Page2.aspx
----------
Response.Write(Request.Form("TextBox1"))
Here, TextBox1 is the ID of the textbox on the Page1.aspx.
Filling Page2.aspx controls with transferred data
What if you want to populate some controls on the Page2.aspx with the form data you received via Server.Transfer method? You can do that simply by making control IDs of Page2.aspx same as Page1.aspx control IDs.
Summary
In this article we examined how Server.Transfer can be used to transfer control from one form to another along with the form data. By setting the EnableViewStateMac attribute of @Page directive we can allow such transfer without any errors.
|
About the Author
|
|
Bipin Joshi |
|
Bipin Joshi is the proprietor of BinaryIntellect Consulting where he conducts premier training programs on .NET technologies. |
|