获取 ASP.NET 页面生命周期的状态
我的方法需要以下功能:如果在 ASP.NET 生命周期的 OnLoad 事件之前调用该方法,则抛出异常,否则继续执行该方法。
我在想这样的事情:
if (Page.LifeCycleState < LifeCycleState.OnLoad) {
throw new InvalidPageStateException();
}
是否可以检索 ASP.NET 页面生命周期的状态?
I need the following functionality in my method: if the method is called before OnLoad
event of ASP.NET life cycle throw an exception else continue execution of the method.
I was thinking of something like this:
if (Page.LifeCycleState < LifeCycleState.OnLoad) {
throw new InvalidPageStateException();
}
Is it possible to retrieve the state of ASP.NET page life cycle?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一种方法是使用您在站点中始终使用的基本页面。这将包含一个名为 PageLoadComplete 的变量,您可以在 PageLoad 事件结束时设置该变量。然后您可以在方法中检查该变量的状态。
如果您想从页面外部的代码(例如 UserControl)访问该变量,则必须将其公开并将您的页面转换为 BasePage。
One approach would be to use a Basepage that you always use in your site. This would contain a variable called PageLoadComplete, which you would set at the end of your PageLoad event. Then you could check the state of this variable from within your method.
If you want to access the variable from code external to your page such as a UserControl, you would have to make it public and cast your page as BasePage.
System.Web.UI.Control 类的实现中有属性(实现):
其中 ControlState 是包含成员的枚举例如:已初始化、ViewStateLoaded、已加载等这里声明
但正如你所看到的,这个属性是内部的。因此,获得控制状态的唯一方法是 Daniel Dyson 提出的。
There is property in a realization of System.Web.UI.Control class(realization):
Where ControlState is enum that contains members such as: Initialized, ViewStateLoaded, Loaded etc. here declaration
But as you can see this property is internal. So only way to get control state is proposed by Daniel Dyson.
通过查看当前
HttpContext
的CurrentHandler
和PreviousHandler
属性,您也许能够找到您要查找的内容。You maybe able to find what you are looking for, by looking at the
CurrentHandler
andPreviousHandler
properties of the currentHttpContext
.目前尚不清楚 Onload 事件的含义,也不清楚“方法”所在的位置。是页面的OnLoad还是控件的OnLoad?是页面的“方法”还是控件的“方法”?
无论如何,我们可以在 Context.Items 字典中存储某种标志,所有控件(包括 Page)在请求期间都可以访问该字典。这消除了使用上面建议的通用基页的需要。
在OnLoad方法中(无论是Page的OnLoad还是Control的OnLoad):
在“方法”中:
It is not clear which Onload event is meant, nor where the "method" resides. Is it the Page's Onload or a Control's OnLoad? Is it a Page's "method" or a Control's "method"?
Anyway, one can store sort of flag in the Context.Items Dictionary, which all controls (including Page) have access to during a request. This eliminates the need to use a general base page like suggested obove.
In the OnLoad method (no matter whether it is a Page's OnLoad or a Control's OnLoad):
In the "method":