开发者

ASP.NET 3.5 Page Load - Design Issue

开发者 https://www.devze.com 2022-12-16 00:02 出处:网络
I have a web site with a series of Master Pages which all derive from a \"Base\" master page. For this example lets call it Mast开发者_如何学JAVAerPage.master.

I have a web site with a series of Master Pages which all derive from a "Base" master page. For this example lets call it Mast开发者_如何学JAVAerPage.master.

Edit-----

Basically there are 2 halves of the website: Public and Private. We'd like to create, among other things, a custom Site Tracking 'utility.' Any time a page is hit... we capture a bunch of data about the user for future reporting, etc.

Right now I am working on the Public site.. but I want to reuse this 'utility' for the private site 3-4 months from now.

To me, a Master Page doesn't seem like the best way of accomplishing this... While I'm still new to .NET, I feel that Master Pages should only be used for organizing the UI.

I've looked into Custom IHttpModule and have been able to get something to work... but its kinda ghetto if you ask me.

public void Init(HttpApplication context)
    {

        context.AcquireRequestState += (new EventHandler(this.context_AquireRequest));
    }
...
void context_AquireRequest(object sender, EventArgs e)
    {

        HttpApplication application = (HttpApplication)sender;
        HttpContext context = application.Context;

            if (context.CurrentHandler is Page){
                //Do Stuff
            }


    }

End Edit----

Is there a better way of having a block of code running each time a page is loaded than the following... Such as a custom IHttpModule or somehow in the Global.asax.

[MasterPage.master] ...

protected void Page_Load(object sender, EventArgs e)
    {


        //...do stuff...

    }

Thanks


If the block of code is always the same then, I would recommend

  • create a base class for all your page
  • write your code in the OnLoad of that page
  • make all your pages inherits from this base class

Here is a code example :

public abstract class PageBase : Page 
{
    protected override void OnLoad(EventArgs e)
    {
      base.OnLoad(e);
      // do stuff
    }
}

All your pages would inherit from this :

public class DisplayOrder : PageBase 
{
    // do stuff
}

Hope, it helps.


The recommended way is using the OnLoad "event".

protected override void OnLoad(EventArgs e)
{
  base.OnLoad(e);
  // do stuff
}

Some backround info on OnLoad vs. Page_Load vs. Load event


Of course we need more info, and you didn't post anything but the method signature for the page load event. That is the event that is fired off when your page loads or an event is clicked or your page is refreshed. The only other thing you could do is add code in your global.asax file.

http://msdn.microsoft.com/en-us/library/2027ewzw.aspx


No - that's really your best bet. If you put it in the base class, it will run for all the child classes as long as you don't override it.

Note that you can have multiple Page_Load handlers, so if you don't want to bother with calling base methods from overridden methods, you can always just add a second handler, although this is kind of an obtuse design. What are you trying to do exactly?


There are a lot of cases where running something every time when a page loads is expected and perfectly acceptable. That's what the Load event is there for. It would ENTIRELY depend on what you're trying to do in your particular case. See this page for an overview of the ASP.Net page life-cycle to ensure that what you're doing follows the basic principle of the event you're doing it in.

0

精彩评论

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