Is it possible to call a write a Page_Unload event in code behind similar to Page_Load event? I wanted to call a method on Page Unload. How do I achieve that?
是否可以在类似于Page_Load事件的代码中调用Page_Unload事件?我想在页面卸载上调用一个方法。我如何实现这一目标?
3 个解决方案
#1
34
With AutoEventWireup
which is turned on by default on a page you can just add methods prepended with **Page_***event* and have ASP.NET connect to the events for you.
使用默认情况下在页面上打开的AutoEventWireup,您可以添加前缀为** Page _ *** event *的方法,并让ASP.NET连接到事件。
In the case of Unload the method signature is:
在卸载的情况下,方法签名是:
protected void Page_Unload(object sender, EventArgs e)
For details see the MSDN article.
有关详细信息,请参阅MSDN文章。
#2
27
Refer to the ASP.NET page lifecycle to help find the right event to override. It really depends what you want to do. But yes, there is an unload event.
请参阅ASP.NET页面生命周期以帮助查找要覆盖的正确事件。这真的取决于你想做什么。但是,有一个卸载事件。
protected override void OnUnload(EventArgs e)
{
base.OnUnload(e);
// your code
}
But just remember (from the above link): 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.
但请记住(从上面的链接):在卸载阶段,页面及其控件已经呈现,因此您无法对响应流进行进一步更改。如果您尝试调用Response.Write方法等方法,该页面将抛出异常。
#3
9
There is an event Page.Unload
. At that moment page is already rendered in HTML and HTML can't be modified. Still, all page objects are available.
有一个事件Page.Unload。此时页面已经以HTML格式呈现,并且无法修改HTML。仍然,所有页面对象都可用。
#1
34
With AutoEventWireup
which is turned on by default on a page you can just add methods prepended with **Page_***event* and have ASP.NET connect to the events for you.
使用默认情况下在页面上打开的AutoEventWireup,您可以添加前缀为** Page _ *** event *的方法,并让ASP.NET连接到事件。
In the case of Unload the method signature is:
在卸载的情况下,方法签名是:
protected void Page_Unload(object sender, EventArgs e)
For details see the MSDN article.
有关详细信息,请参阅MSDN文章。
#2
27
Refer to the ASP.NET page lifecycle to help find the right event to override. It really depends what you want to do. But yes, there is an unload event.
请参阅ASP.NET页面生命周期以帮助查找要覆盖的正确事件。这真的取决于你想做什么。但是,有一个卸载事件。
protected override void OnUnload(EventArgs e)
{
base.OnUnload(e);
// your code
}
But just remember (from the above link): 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.
但请记住(从上面的链接):在卸载阶段,页面及其控件已经呈现,因此您无法对响应流进行进一步更改。如果您尝试调用Response.Write方法等方法,该页面将抛出异常。
#3
9
There is an event Page.Unload
. At that moment page is already rendered in HTML and HTML can't be modified. Still, all page objects are available.
有一个事件Page.Unload。此时页面已经以HTML格式呈现,并且无法修改HTML。仍然,所有页面对象都可用。