I am using Respose.Write
on Page_Unload
event, then I get the error
Response is not available in this context.
May I know why we can't use?
protected void Page_Unload(object sender, System.EventArgs e)
{
Response.Write(" hi ");
}
From the MSDN article on the ASP.NET Page Lifecyle :
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 .
So what you're trying to do is actually unsupported according to the documentation.
this logically makes sense. As the page is being unloaded, the browser has no need for further response from that page . This is really just where cleanup is performed.
Some common use cases are also mentioned on MSDN:
For user controls:
...use this event to do final cleanup for specific controls, such as closing control-specific database connections.
And for the page as a whole:
...use this event to do final cleanup work, such as closing open files and database connections, or finishing up logging or other request-specific tasks.
That is because the Unload
event is triggered after the page has been rendered. The page is already complete and on the way to the browser, there is no longer a response stream that you can write to.
The Unload event comes after the page has been sent back to the client. It is too late to write to the Response then.
Refer to the ASP.NET Page Life Cycle documentation.
Remember - 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
.
At that moment page is already rendered in HTML and HTML can't be modified. Still, all page objects are available.
Refer to the ASP.NET page lifecycle
Read up on the page life cycle: http://msdn.microsoft.com/en-us/library/ms178472.aspx
UnLoad comes after rendering, so at this point you have outputted all you can to the screen.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.