检查页面事件是否触发
本文关键字:是否 事件 检查 | 更新日期: 2023-09-27 18:02:10
我知道这可能是一个有点奇怪的问题,但是给定一个Page类,看起来像这样:
public class abc:Control
{
public abc()
{
this.Init+=new EventHandler(foo);
this.Load+=new EventHandler(foo);
}
void foo(object sender, eventargs e)
{
//determine whether or not this.Init has already fired.
}
}
我知道我可以通过设置全局布尔值'hasinitfired'来做到这一点,但我想知道这是否没有必要,或者如果某些东西作为。net库的一部分已经存在。
为什么需要知道init是否被触发。Init总是在回发或回调期间被触发。通过ASP.net页面生命周期,您将了解在init之后触发的所有事件和之前触发的所有事件。如果您打算对不同的事件使用相同的处理程序,可以创建一个类变量来标识当前事件。我建议附加不同的处理程序,并使用不同的参数值调用另一个方法。
,
public class abc:Control
{
public abc()
{
this.Init+=new EventHandler(foo1);
this.Load+=new EventHandler(foo2);
}
void foo1(object sender, eventargs e)
{
foo('init');
}
void foo2(object sender, eventargs e)
{
foo('load');
}
void foo(string from)
{
// do something
}
}
这将提供更干净的解决方案和灵活性来添加功能。
我猜你想知道init是否在foo运行时被触发。就像@Adam说的,如果你想知道你的应用在做什么,追踪可以让你这样做。
当它运行时,我认为最好的方法是像你建议的那样带一个标志。
西蒙这看起来像您将使用。net Trace
类。您可以使用trace在Visual Studio输出窗口中放置文本。
Trace.WriteLine("Method called.");
更进一步,你可以使用PostSharp方面来修饰这个方法,但这是一个第三方库。