-1

I am working on a legacy service that has 30 aspx pages with years of business logic

There is a parent aspx page which acts as a base page that the above aspx pages inherit from.

I need to add a Response header and I am having trouble finding the right place to add it to the ParentPage.aspx.cs so that it gets applied to all aspx pages.

Ideally I would want to add after PageLoad() is done for the the data to be available to add to the header.

I tried using the onPreRender() and onLoadComplete() stages to add the header. But it is not guaranteed that this would get called because the aspx pages have logic that do Response.Redirect() Because of the redirect onPreRender() and onLoadComplete() do not get called. I would not be able to change the logic on redirects

However UnLoad() does get called all the time. But response cannot be altered in the UnLoad() stage

Are there any suggestions where the header to the response could be added in the ParentPage.aspx.cs ?

user3451476
  • 297
  • 1
  • 4
  • 17

1 Answers1

2

Do you have a master page?

Just drop in the code to add a header in the page load event.

I mean, code can fill out text boxes, change things, and then you can toss in a new header - the order of such events don't matter.

So, if you have a master page that all pages use?

Then do this in site master:

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.AddHeader("Test", "My Test Value");
    }

So, it not clear if you have a master page, but page load triggers in site master each time any navigate to any page occurs. And also the above runs each time any post-back or button click occurs on any page.

So, just add the header in the page load in master.

The above thus results in this:

enter image description here

So, it don't matter if you add the header at the start of code, or end of code say in a page. The other code behind can run, make changes to the "DOM" via code behind, but everything WAITS 100% until ALL of the code behind is done running, and THEN and only THEN does the WHOLE page make the trip back down to the client side, the client side re-loads the page, re-starts any JavaScript code (and re-sets, all JavaScript values and variable to fresh start), and then the page life cycle is complete, ready to start again on the next button click etc. And on the server side, the page class is disposed, and all its variables etc. also goes out of scope (hence the term state-less). So, both ends, including client side js code variables are re-set in this process.

So, in fact the "order" or what event you use to inject/add that header? it really don't matter when it runs, only as long as it runs at "some point" in time during that whole page life cycle on the server.

In fact, what I am saying, it is REALLY hard to mess this up, since just about any event can add the header - but page load looks to be as good as any event here.

Edit: The page load event

You thus should see/be able to add the event to page master like this:

enter image description here

Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51
  • Thank you for the details. There is a MasterPage.aspx.cs The MasterPage does not have a Page_Load(). It only has a OnLoad() However, all the sub pages have a Page_Load() method. Are you referring to add the header in the OnLoad() method of MasterPage – user3451476 Jan 28 '23 at 22:01
  • 1
    yes, open the site master page, go to code behind for that page. It MOST certainly should/would/does/normally/in most cases will have a page load event. I suppose if it does not, then add one. But yes, both page load in master page, and the page load event in each child page fires each time. So, yes, in code behind for Site.Master.cs should have a page load event as per above example. – Albert D. Kallal Jan 28 '23 at 22:08
  • The code behind for MasterPage in my case only has a OnLoad() event and does not have a PageLoad() event – user3451476 Jan 28 '23 at 22:14
  • 1
    see my edit, and screen shot - you should see much the same events for regular pages as you would/do for the master page. – Albert D. Kallal Jan 28 '23 at 22:18
  • thanks for the screenshot. I only realized now that there is no Master site. It only has a ParentPage.aspx that all child pages inherit from. Is there a way to achieve this without the Master site ? – user3451476 Jan 28 '23 at 22:20
  • Well, it possible that ParentPage is the same thing here. If all pages are child of that page, then that in "effect" is your master page, and thus yes, use the page load event of that ParentPage. I mean, does each child page have a "form" (markup) area for each page, or does each page only have "content place" holders. If all such pages only have "content place holders", then that is your master page, and thus again, page load should/would work fine. – Albert D. Kallal Jan 28 '23 at 22:24