maxp
maxp

Reputation: 25171

Accessing Request or Response OnUnload()?

I want to store a cookie using something like

Response.Cookies.Set(new HttpCookie("name","value");

after I have finished the page lifecycle, so it makes sense to put it in the OnUnload() event.

However at this stage, Request and Response have already been unloaded, so throw a null ref exception.

Has anybody got any brain storms to get around this?

All i can think of is putting it in OnPreRender(), but am worried this may be too 'soon'.

Upvotes: 2

Views: 369

Answers (1)

Guffa
Guffa

Reputation: 700810

As the cookies are sent in the HTTP header, you have to set the cookie before anything is written to the response stream. Thus, you have to set the cookie before the page is rendered, as that is what's generating the code that is sent in the response.

So, adding a cookie after the page life cycle is way too late. Why do you think that it makes any difference when you add the cookie to the page anyway? As cookies are sent in the HTTP header, they will arrive to the browser at the same time regardless of when you run the code to add them.

Upvotes: 2

Related Questions