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

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

    Requires Free Membership to View

    When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to provide a unique online resource for developers, architects and development managers tasked with building and maintaining enterprise applications using Visual Basic, C# and the Microsoft .NET platform.

    Hannah Smalltree, Editorial Director

    By submitting your registration information to SearchWinDevelopment.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchWinDevelopment.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

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)"

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.

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:

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, System.Web.UI.Page)
            Response.Write(sourcePage.TextBox1.Text)

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.

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

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

This was first published in February 2003

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.