Search Articles :

HyperLink
AddThis Feed Button
Bookmark and Share



Latest Downloads
Jobs Site Starter Kit for ASP.NET 3.5
Store documents in on-line briefcase
Easy Survey
Share photos with your own Photo Gallery
Web Site File Manager
BinaryIntellect Code Generator (Beta)
Develop your own Blog
E-commerce Starter Kit
Jobs Site Starter Kit
Database Helper for .NET 2.0
 
Latest Blog Posts
Using Extension Methods
Working with Binary Large Objects (BLOBs) Using SQL Server and ADO.NET
भाग 4 : मी नोकरी सोडतो
Using LINQ in ASP.NET (Part 4)
देवाच्या डाव्या हाती (भाग 3)
Using LINQ in ASP.NET (Part 3)
देवाच्या डाव्या हाती (भाग 2)
Using LINQ in ASP.NET (Part 2)
Beginning XML with C# 2008: From Novice to Professional Published!
देवाच्या डाव्या हाती (भाग 1)
 




Hosted By DiscountASP.net
Developer's Guide to ASP.NET 3.5
Master ASP.NET 3.5 development using C# and Visual Studio.NET 2008. Web forms, server controls, data binding, AJAX, ASMX and WCF services and more...
Beat stress - learn Kriya Yoga Meditations
A six week online course in our style of Kriya Yoga is available absolutely FREE. Very few websites teach you the ancient art and science of Kriya Yoga in such a systematic and authentic manner.
<%@ 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.

Associated Links
Download Source Code

Posted On : 22 Mar 2003
Current Rating :
Rate This Article :

About the Author
Bipin Joshi
Bipin Joshi is the proprietor of BinaryIntellect Consulting where he conducts premier training programs on .NET technologies. He wears many hats including software consultant, mentor, prolific author, webmaster, Microsoft MVP and member of ASPInsiders. Having adopted Yoga way of life Bipin also teaches Kriya Yoga to the interested individuals. His detailed profile can be read at his blog. He can also be reached there.



Copyright (C) BinaryIntellect Consulting. All rights reserved.
Contact Us
Read Terms Of Use