【发布时间】:2011-10-26 17:16:45
【问题描述】:
对于我的应用程序,我有一个页面通过Server.Transfer 重定向到另一个页面(在同一应用程序中)。我需要这样做,因为原始页面有一个我需要使用 Page.PreviousPage 属性访问的对象。
一旦我的“目标”页面完全加载,当我执行回发时,我对源页面对象制作的本地深度克隆会突然从内存中释放?这是设计使然 - 与 Server.Transfer 有关吗?
一个例子...
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"
此时,一切正常。东西被正确克隆了,一切都很好。
(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!
问题就在这里。我的猜测是 Server.Transfer 在新页面加载后停止与源页面的任何关联。
有没有更好的跨页面对象传递方式?
【问题讨论】:
-
如果您在该页面上放置一个按钮和服务器端点击,您可以从点击事件中访问您的对象吗?你能显示一些代码吗?
-
我说的可能有点不清楚,但我不知道有什么其他的说法。
-
这对我来说很难闻。您当然可以将原始对象存储在其他容器中,然后在重定向页面的页面加载中将其拉回?
-
@Graham 我认为这就是问题所在。我不确定哪种“容器”最适合我的需要。