Microsoft Knowledge Base Email Alertz

When you use HttpServerUtility.Transfer("

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 316920 - Last Review: April 19, 2007 - Revision: 3.2

PRB: "View State Is Invalid" Error Message When You Use Server.Transfer

This article was previously published under Q316920

On This Page

SYMPTOMS

When you use HttpServerUtility.Transfer("page name", true), you receive the following error message:
The View State is invalid for this page and might be corrupted

CAUSE

This problem occurs because the EnableViewStateMac attribute of the <pages> element is set to true by default. When this attribute is set to true, ASP.NET runs a message authentication check (MAC) on the view state of the page when the page is posted back from the client. This check determines if the view state of the page was modified on the client. For security purposes, it is recommended that you keep this attribute set to true.

When you call the Server.Transfer method and set the second parameter to true, you preserve the QueryString and the Form collections. One of the form fields is the hidden __VIEWSTATE form field, which holds the view state for the page. The view state message authentication check fails because the message authentication check only checks each page. Therefore, the view state from the page that calls Server.Transfer is not valid on the destination page.

View state is page scoped and is valid for that page only. View state should not be transferred across pages.

RESOLUTION

To resolve this problem, use one of the following methods.

Resolution 1

Transfer values between pages to pass your server control values to the other pages. For more information, refer to the following MSDN documentation:
Passing Server Control Values Between Pages
http://msdn2.microsoft.com/en-us/library/6c3yckfw(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/6c3yckfw(vs.71).aspx)
This requires that you create public properties for each property of a control that you want to access from the destination page.

If you have many controls, and if you want to access the properties of these controls from another page, you can also declare those controls as public variables. For example:

Page1.aspx
Public Class Page1
    Public WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

    'Insert your code here.
End Class
				
Page2.aspx
            Dim sourcePage As Page1
            sourcePage = CType(Context.Handler, WebForm1)
            Response.Write(sourcePage.TextBox1.Text)
				

Resolution 2

Do not pass the second parameter (which is false by default) when you call Server.Transfer. For example:
Server.Transfer("<page name>")
				
This code does not send the QueryString and the Form fields to the page that is called. When no data is transferred, ASP.NET does not run the message authentication check.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create an .aspx page named WebForm1.aspx that transfers execution to another page. Add the following code to WebForm1.aspx:
    <%@ Page language="vb" AutoEventWireup="true" %>
    
    <html>  
      <body>	
        <form id="WebForm1" method="post" runat="server">
          <asp:TextBox id="txtName" runat="server">Your Name</asp:TextBox><br>
          <asp:Button id="Button1" runat="server" Text="Submit" OnClick="Button1_Click"></asp:Button>
        </form>	
      </body>
    </html>
    
    <script runat=server>
    Sub Button1_Click(sender As Object, e As System.EventArgs)
    	Server.Transfer("WebForm2.aspx",true)
    End Sub
    
    </script>
    					
  2. Create another .aspx page named WebForm2.aspx, and then add the following code:
    <%@ Page language="vb" AutoEventWireup="true" %>
    
    <html>
      <body>	
        <form id="WebForm2" method="post" runat="server">
          <asp:Label id="lblName" runat="server" >Web Form 2</asp:Label>
        </form>
    	
      </body>
    </html>
    
    <script runat=server>
    Sub Page_Load(sender As Object, e As EventArgs)
    
    Dim thisPage As System.Web.UI.Page
    Dim nameTextBox As TextBox
    
    	thisPage = CType(Context.Handler, System.Web.UI.Page)
    	nameTextBox =  CType(thisPage.FindControl("txtName"), System.Web.UI.Control)
    
    	lblName.Text = "Your name is '" & nameTextBox.Text & "'."	
    	
    End Sub
    
    </script>
    					
  3. Open WebForm1.aspx in your browser, and then click Submit.

APPLIES TO
  • Microsoft ASP.NET 1.0
Keywords: 
kbprb kbstate kbwebforms KB316920
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate