No page_PreLoad on the main page?

I have a custom object in a session that is updated using postbacks on both the main page and the main page.

After the postback, I need to get the Session object, rebuild and change parts or all of the object, and load it back into the session.

I wrote this code in Page_Load of the main page. It works fine on only one page.

Now I have written another page with the same main page, and I want the main page to be able to modify my session object as before.

Therefore, it seemed to me that I just needed to move the session processing code to the main Page_Load page. But this did not work as I expected, since the control on the main page (namely, the relay) accesses the session object in the OnItemDataBound event handler before the Page_Load master page is launched, and thus it receives only the previous state of the session object, (This is true only for the repeater on the main page, the repeater on the main page receives the current state when it accesses the session)

Regardless, I thought I could use the Page_PreLoad event of the main page, I could access the postback data in Page_PreLoad just as well, and update the session object accordingly, but I found that there was no Page_PreLoad page on the main page, or I can not use it.

Where should I update my object in a session?

To summarize: I need a place in the masterpage codebehind where the postback data is ready for use, and not one of the main page or MasterPage OnItemDataBound repeater event has yet been triggered.

+4
source share
4 answers

I hope I get it right - I think your best option is to create a base page and make your pages inheritable. Put your logic in the Page_Load or Page_PreLoad event handler on the base page. Main pages load after loading the actual page.

So you create the base page:

 public class BasePage : Page { protected void Page_Load(object sender, EventArgs e) { // sesion logic here } } 

And make your page inherited from this (and also using your main page):

 public class Page1 : BasePage //instead of Page { protected void Page_Load(object sender, EventArgs e) { base.Page_Load(sender, e); } } 
+3
source

There is also another solution: in the init event of the main page, you can subscribe to the page preload event. view this code on the main page:

  protected override void OnInit(EventArgs e) { base.OnInit(e); Page.PreLoad += OnPreLoad; } protected void OnPreLoad(object sender, EventArgs e) { //this function is in the masterpage but will be called on page preload event so do here your preload stuff ... } 
+3
source

From the asp.net Forums :

The main "pages" are UserControl hosted by the actual Page . This means that they only perform the management life cycle, not the page .

As expected, the base Page class is the best approach, so you might have common code shared between pages. Another is to use one of the Request events supported by the Application class.

i ended up just using the Init event (since there is no PreLoad event):

 protected void Page_Init(object sender, EventArgs e) { if (Request.Params["debug"] != null) Page.Trace.IsEnabled = true; } 
+2
source

you can also override preload if you are looking for this:

 public class BasePage : System.Web.UI.Page { public BasePage() { } protected override void OnPreInit(EventArgs e) { MasterPageFile = "MasterPage2.master"; } } 
+1
source

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


All Articles