For my application, I have a page that redirects to another page (within the same application) via Server.Transfer. I need to do this because the original page has an object that I need to access by using the Page.PreviousPage property.
Once my “destination” page has been fully loaded, a local deep clone that I made of the source page’s object is suddenly released from memory once I perform a postback? Is this by design–something to do with the Server.Transfer?
An example…
Page1.aspx:
Public Structure myCustomObject
Implements ICloneable
Dim someField as String = "default value" ' Default value
Public Function Clone() As Object Implements System.ICloneable.Clone
Dim temp as new myCustomObject
temp.someField = Me.someField
Return temp
End Function
End Structure
Dim obj As myCustomObject
Public ReadOnly Property objProp as myCustomObject
Get
Return obj
End Get
End Property
objProp.someField = "changed value from source page"
Server.Transfer("page2.aspx", True)
Page2.aspx:
(onLoad)
Dim newObj As myCustomObject
newObj = Page.PreviousPage.objProp.Clone()
Debug.Write(newObj.someField) ' Output: "changed value from source page"
At this point, EVERYTHING works as it should. Stuff got cloned over correctly and all is well.
(Let's say this is on a button click event)
Debug.Write(newObj.someField) ' Output: "default value"<- This is NOT "changed value from source page" for some reason when it was working literally a few lines ago!
It’s around here that I get the problem. My guess is that the Server.Transfer stops any association with the source page after the new page loads.
Is there a better way for cross-page object passing?
Just pass a variable in the
HttpContext, you will have to handle your casting, not sure whatPage.PreviousPageis:Current Page:
Transfered to page:
Sorry for the C#, there was no code and the question wasn’t marked with VB.NET when I answered. Someone feel free to convert.