ASP.NET 3.5 Page Load - Design Problem

I have a website with a series of master pages, all of which are produced on the "Base" main page. In this example, you can call MasterPage.master.

Edit -----

Basically there are two halves of the website: public and private. We would like to create, among other things, a special website tracking utility. Every time a page hits ... we collect a bunch of user data for future reports, etc.

Now I work on a public site .. but I want to reuse this "utility" for a private site in 3-4 months.

For me, the main page does not seem to be the best way to do this. Although I'm still new to .NET, I feel that master pages should only be used to organize the user interface.

I studied the custom IHttpModule and was able to get something to work ... but my curious 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
            }


    }

Edit End ----

Is there a better way to have a code block executed each time a page loads than the following ... For example, a custom IHttpModule or somehow in Global.asax.

[MasterPage.master] ...

protected void Page_Load(object sender, EventArgs e)
    {


        //...do stuff...

    }

thank

+3
source share
5 answers

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

  • create a base class for all your pages
  • write your code in the onload of this page

:

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

:

public class DisplayOrder : PageBase 
{
    // do stuff
}

, .

+1

- OnLoad ".

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

OnLoad vs. Page_Load

+1
0

- . , , .

, Page_Load, , , , . ?

0

There are many cases where starting something every time a page loads is expected and quite acceptable. This is an event Load. It will be completely dependent on what you are trying to do in your particular case. See this page for an overview of the life cycle of an ASP.Net page to ensure that what you do is in line with the basic principle of the event you are doing it.

0
source

Source: https://habr.com/ru/post/1727383/


All Articles