Problem solve Get help with specific problems with your technologies, process and projects.

Pass your server control values to other pages in ASP.NET

How to transfer values between pages to pass your server control values to other pages in ASP.NET.

This tip was submitted by member Pankaj Khullar. Let other users know how useful this tip is by rating it below. Got a tip or code of your own you'd like to share? Submit it here!

ASP.NET developers often encounter the problem of transferring server side control values from one page to another using Server.Transfer ("PageName", true) method. Usual syntax of Request.Form doesn't work if you try to access the value of server side control from the redirected page. Developers often encounter this error:

"View state is invalid and might be corrupted error message when you use Server.Transfer ("page name", true)"


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.

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

Resolution 1: 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:

Public Class Page1
    Public WithEvents TextBox1 as System.Web.UI.WebControls.TextBox

    'Insert your code here.
End Class
            Dim sourcePage As Page1
            SourcePage = CType (Context.Handler, System.Web.UI.Page)

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.

Resolution 2: This option doesn't require controls to be defined as public variables.

Public Class Page1
    Protected WithEvents TextBox1 as System.Web.UI.WebControls.TextBox

    'Insert your code here.
End Class
            Dim sourcePage As Page1
     Dim sourceControl as TextBox
     sourcePage = CType(Context.Handler, System.Web.UI.Page)
    SourceControl = CType
     Response.Write (SourceControl.Text)

Dig Deeper on Win Development Resources

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.