can we access viewstate and session objects at unload event of page.
3 Answers
The "unload" event is called as soon as the page response has been sent to the client browser, there's usually nothing useful you can do in this event, other than clear up resources (i.e. file/database handles, etc).
Unload is called after the page has been fully rendered, sent to the client, and is ready to be discarded. At this point, page properties such as Response and Request are unloaded and any cleanup is performed.
[...]
During the unload stage, the page and its controls have been rendered, so you cannot make further changes to the response stream. If you attempt to call a method such as the Response.Write method, the page will throw an exception. (From ASP.NET Page Life Cycle Overview).
You might possibly be able to read viewstate, you certainly can't change it, and the same propbably holds true for session. What are you trying to achieve?

- 1
- 1

- 26,785
- 5
- 80
- 117
yes offcourse, try the following :
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.Load
Session("test") = "testing"
End Sub
Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
Dim str As String = Session("test")
Response.Write(str)
End Sub

- 5,278
- 6
- 41
- 66
-
Calling Response.Write in the unload event will result in an exception - the page has already been sent to the client by that point. – Zhaph - Ben Duguid Jan 08 '10 at 11:13
In another event, save the viewstate you want to save.
Then in the Page_Unload
event, call this.SaveViewState()
. The view state you already saved will be updated with any changes.
The reason I needed this was to capture input from users in dynamically created controls.

- 5,315
- 3
- 31
- 51