C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Don't preserve viewstate when doing a Server.Transfer


Posted Date: 15 Mar 2004    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: Satheesh SugumaranMember Level: Gold    
Rating: 1 out of 5Points: 5



The ASP.NET version of the Server.Transfer method accepts a second argument which, if true, causes the query string and all the form's fields to be transferred and made available to the destination page. However, when the destination page executes, the following error is likely to be generated:

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

The reason for this error is that the EnableViewStateMac attribute of the element of Web.config is set to TRUE by default. This causes a message authentication check (MAC) on the view state of the page being posted back. The view state is held in the __VIEWSTATE hidden field, and the MAC fails because the MAC only checks each page. In other words, the view state of the page where Server.Transfer is called isn't valid on the destination page.
We can avoid this problem by not passing True as the second argument of the Server.Transfer method. However, you must devise alternative ways to make the query string and controls' values of the source page available to the destination page, if these values must be processed by the destination page. There are two ways to achive this. The easiest way is to make all controls on the source page public, as in:

Public Class WebForm1
Public WithEvents txtUserName As System.Web.UI.WebControls.TextBox
' ...
End Class


A safer way is to wrap these control's values in properties:

Public Class WebForm1
Private WithEvents txtUserName As System.Web.UI.WebControls.TextBox
' ...

ReadOnly Property UserName() As String
Get
Return Me.txtUserName.Text
End Get
End Property
End Class


In both cases you can then access the actual value from the destination page as follows:

' inside the destination page
Dim sourcePage As WebPage1 = DirectCast(Context.Handler, WebForm1)
Dim userName As String = sourcePage.UserName






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: MailMessage Class - Properties
Previous Resource: Web.Config
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use