开发者

Checking if page event fired

Im aware this may be a slightly odd question, but given a Page class that looks like this:

    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.
      }
    }

I know I could do this by setting a global boolean 'hasinitfired', but I was wondering if this was开发者_如何学Python not necessary or if something as part of the .net library already existed.


Why do you need to know init is fired or not. Init always gets fired during postback or callback. Go through ASP.net page lifecycle, and you will know what all events fired after init and what all before. If you intend to use same handler for different events, yes make a class variable to identify the current event. I recommend attach different handler, and call another method with different param value.

like,

 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
      }

    }

This will give cleaner solution and flexibility to add functionality.


I'm guessing you want to know if init has fired when foo runs. As @Adam said, tracing would let you do that if you wanted to see what your app was doing.

Whilst it's running, the best way as I see it would be with a flag, as you suggested.

Simon


This looks like something you'd use the .NET Trace class for. You can use trace to put text in the Visual Studio output window.

Trace.WriteLine("Method called.");

Tracing in .NET has more options than my example:

http://www.15seconds.com/issue/020910.htm

Even further to this, you could use a PostSharp aspect to decorate this method, but that is a third party library.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜