Home > Microsoft .Net Development Tips > ASP.NET Development > Pass your server control values to other pages in ASP.NET
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ASP.NET DEVELOPMENT

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


Pankaj Khullar
02.16.2003
Rating: -2.67- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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

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)

Rate this Tip
To rate tips, you must be a member of SearchWinDevelopment.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
ASP.NET Development
How to use jQuery to solve Javascript browser compatibility problems
How to write an out-of-browser Silverlight 3 application in 3 steps
Silverlight 3 beta SDK download lets developers try new RIA features
Visual Studio's IntelliSense for jQuery doesn't autocomplete correctly
Dundas Map for .NET kicks up geographic visualization
Return to CodePlex: Into the Sandcastle…
VBScript Tutorial
Use PHP with Visual Studio to create Web sites
Visual Studio Team System Add-ins: Conchango Scrum for Team System and Scrum Dashboard
Visual Studio 2008 and .NET Framework 3.5 SP1 introduces ADO .NET Entity Designer

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

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.



Database Programming Solutions - .NET XML, Visual Studio LINQ, ORM .NET
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts