Prevent "The View State is invalid for this page and might be corrupted" Error

When we use server.transfer method to navigate to next page, at times we give value; as the second parameter for the method. In this cases we tend to get the error messageThe View State

ITPro Today

March 3, 2004

1 Min Read
ITPro Today logo in a gray background | ITPro Today

When we use server.transfer method to navigate to next page, at times we give value; as the second parameter for the method. In this cases we tend to get the error message

The View State is invalid for this page and might be corrupted .“

This is because the view state of the page where Server.Transfer is called isn't valid on the destination page.

To avoid this problem  do not pass “True” as the second parameter for the Server.Transfer method. An easy and safer way to make the values of the source page available to the destination page, is to wrap these control's values in properties:
Public Class WebForm1

Private WithEvents txtloginID As System.Web.UI.WebControls.TextBox
   ' ...

    ReadOnly Property LoginID() As String
        Get
            Return Me.txtloginID.Text
        End Get
    End Property
End Class

Inside the destination page you can access the values of the source page as

Dim source As WebPage1 = DirectCast(Context.Handler, WebForm1)
Dim loginID As String = source.loginID.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like