Solved

Page_Preload event is not firing

Posted on 2009-04-10
15
2,275 Views
Last Modified: 2012-05-06
I created a page class which is a custom class derived from System.Web.UI.Page.
and all the web pages in my application are derived from this page class only. so that any theme/culture settings done in page class will effect to all web pages in my application. you can understand more clearly by observing the below code.

namespace Survey.Code.Utilities
{
public class Page : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, System.EventArgs e)
        {
                   this.Theme = Session[SessionData.LoggedEmpTheme].ToString();
        }
     protected void Page_Preload(object sender, EventArgs e)
        {
        }
}
}

 public partial class OrderSurveys : Survey.Code.Utilities.Page
{
}
here the OrderSurveys is my web page.

what my problem is suppose when i request OrderSurveys.aspx  Page_PreInit and
Page_Preload events of pages.cs are calling properly. I checked this by putting break points in vss. i copied the url(http://localhost/surveys/OrderSurveys.aspx )  and requested some other aspx page. now pasted the url which i have copied previously and clicked on go to. i navigated to OrderSurveys.aspx  page but this time Page_PreInit and Page_Preload events of pages.cs  are not firing. i want this events to be fire on every request made to OrderSurveys.aspx  .

i think that caching is applied to OrderSurveys.aspx and i disabled the caching for the entire application using outputcache tag in web.config. But still my problem is not resolved.
the way i followed to disable caching is
<caching>
      <outputCache enableOutputCache="false" enableFragmentCache="false"
                   sendCacheControlHeader="false" omitVaryStar="false"/>
    </caching>

Is my problem with caching or any thing else. provide me the best way to resolve my problem.





0
Comment
Question by:ravindra333
  • 7
  • 7
15 Comments
 
LVL 5

Expert Comment

by:brandonvmoore
ID: 24114290
I'm pretty sure caching is not your problem, but if you wanted to make sure you could call the page with arbitrary values in the url (ie. www.website.aspx?randomnumber=23432).  Create a function to generate a new random number for each call and then you won't get a cached page.
0
 
LVL 39

Expert Comment

by:abel
ID: 24114498
> and requested some other aspx page. now pasted the url which i have copied previously and clicked on go to. i navigated to OrderSurveys.aspx  page but this time Page_PreInit and Page_Preload events of pages.cs  are not firing.

first I thought you meant that the page is reloaded with a roundtrip to the server, but not all events are hit. On second read, I understand that you mean that sometimes no events at all are firing. This is a browser caching issue, not a server caching issue (you have set the server caching features in the web.config). To make sure that the browser does not cache your page you should use:

   Response.Cache.SetNoStore();

somewhere in your page_load. Then, first you'll have to refresh your browser with Ctrl-F5. After that, the page will not be cached anymore.

-- Abel --


0
 

Author Comment

by:ravindra333
ID: 24116499
hi abel

Yes i agree with you this is a browser cache issue only. but if we use  Response.Cache.SetNoStore(); then we have to update all the pages in my web application. There are more than 150 pages in my application.
Other than this can we do any thing globally such that it will effect the entire application.
0
 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24118596
If you really want to set no-store to every single request to your application (which I personally consider a bad idea, users won't like the slow response and your server and network get a much higher load), you can do several things, of which I consider the following two the most workable in practice:
  1. Set the no-store in your master-page. Most large sites have a hierarchical structure where each master inherits from one root master and each page inherits from these masters. If that is the case for you, just set the Response.Cache.SetNoStore() inside the Page_Load of your root master page or of any master page that is used for non-cacheable pages (this gives the highest flexibility).
  2. Set the no-store in your Application_BeginRequest of your global.asax. This way, every single request to any page in your site, will always have the no-store set (this does not give flexibility, but is an "implement once, use always" approach).
Use the first approach if you want some more control. You can use master overrides through master page properties which you can call from your normal pages, if you want to be able to override the setting from a single page.

Use the second approach if you want to always disallow caching of your pages.

-- Abel --
0
 

Author Comment

by:ravindra333
ID: 24128143
hi abel

My problem is not resolved eventhough i set  no-store in my Application_BeginRequest of my global.asax
still the page caching at browser side.
0
 

Author Comment

by:ravindra333
ID: 24128218
hi abel

My problem is not resolved eventhough i set  no-store in my Application_BeginRequest of my global.asax
still the page caching at browser side.
0
 
LVL 39

Expert Comment

by:abel
ID: 24128435
I just tried it myself and I chose a slightly different spot, namely at the end of the handling of the request, just before the headers are send to the browser. That means that any code in your pages that may change the Cache handling is overridden:

protected void Application_PreSendRequestHeaders(object sender, EventArgs e)

{

    Response.Cache.SetNoStore();

}

Open in new window

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:ravindra333
ID: 24128558
hi abel

Still my problem is not resolved eventhough i set  no-store in my Application_PreSendRequestHeaders of my global.asax
still the the events in page.cs are not firing.
the scenario i tested is
I logged in as employee1 and requested a aspx page (i got the requested page and alsot the events in page.cs are firing) and copied the url and logged out. And again logged in as employee2 requested the copied url i am getting the page but the events in page.cs are not firing.
0
 
LVL 39

Expert Comment

by:abel
ID: 24128606
I'm not sure what you mean by "copy". I created a small web page to test the scenario. The cache NoStore methods work for IE , Opera and FF. If you use a different browser, you can also try setting the ExpiryDate.

While you are testing, please set a breakpoint on both the Page_Load and the Application_PreSendRequestHeaders, so that you are sure both are called. I don't know how your page_load is called and whether it contains code that may react on the request.

You seem to have problems with being Employee1/Employee2. Maybe you mean that you see the page of Employee1 where you expect to see the page of Employee2 (because you logged in/out/in). Depending on how you deal with cookies, sessions (inproc/client/server side/stateserver etc), this problem may need to be solved in a totally different way.

To know what events are really firing, turn Traceing on.

-- Abel --
0
 
LVL 39

Expert Comment

by:abel
ID: 24128608
Btw: how does the URL look that you copied? Does it contain something like the userid, a sessionid, or something else that may make it to show the Employee1's page?
0
 

Author Comment

by:ravindra333
ID: 24128757
Copy in the scense i just copy the requested url in the address tab of browser.

The Page_Load contains a code to check the accessbality of the page for the logged employee.
If the logged employee has no rights to access the requested page then i redirect him to Erorpage.aspx
otherwise i will give access to him.
 actually OrderSurveys.aspx can be accessed by only employee1 but not by employee2.
I am logged in  as emplyee1 and accessed OrderSurveys.aspx. The requested url is
http://localhost/McQuaigNewGen/Web/UI/Organization/OrderSurveys.aspx?Survey=Order . i cpoied this url and  logged  in as employee2 and requested cpoied url. Application_PreSendRequestHeaders is firing
but the Page_Load of page.cs is not firing.
i want to redirect him to ErrorPage.aspx because employee2 don't have access to OrderSurveys.aspx .this is posible only when page_load is firing.
if i close the browser and logged in as employee2 and requested the url http://localhost/McQuaigNewGen/Web/UI/Organization/OrderSurveys.aspx?Survey=Order then both
the events Application_PreSendRequestHeaders and page_load of pages.cs  are firing and i am able to redirect employee2 to Errorpage.aspx . i am facing problem when logged in as employee1 and immediately logged out and checkig the accessbility of employee1 page by logged in as employee2.
0
 

Author Comment

by:ravindra333
ID: 24128872
Copy in the scense i just copy the requested url in the address tab of browser.

The Page_Load contains a code to check the accessbality of the page for the logged employee.
If the logged employee has no rights to access the requested page then i redirect him to Erorpage.aspx
otherwise i will give access to him.
 actually OrderSurveys.aspx can be accessed by only employee1 but not by employee2.
I am logged in  as emplyee1 and accessed OrderSurveys.aspx. The requested url is
http://localhost/McQuaigNewGen/Web/UI/Organization/OrderSurveys.aspx?Survey=Order . i cpoied this url and  logged  in as employee2 and requested cpoied url. Application_PreSendRequestHeaders
and the Page_Load of page.cs are not firing.
i want to redirect him to ErrorPage.aspx because employee2 don't have access to OrderSurveys.aspx .this is posible only when page_load is firing.
if i close the browser and logged in as employee2 and requested the url http://localhost/McQuaigNewGen/Web/UI/Organization/OrderSurveys.aspx?Survey=Order then both
the events Application_PreSendRequestHeaders and page_load of pages.cs  are firing and i am able to redirect employee2 to Errorpage.aspx . i am facing problem when logged in as employee1 and immediately logged out and checkig the accessbility of employee1 page by logged in as employee2.
0
 

Author Comment

by:ravindra333
ID: 24136058
hi abel i have a small problem i need your help

let us consider SurveyTasks.aspx is my parent page. Changelanguae.aspx is my popup. Uisng Window.Open i am opening the popup Changelanguae.aspx.
so the opener for the Changelanguae.aspx  is SurveyTasks.aspx . we can find whether Changelanguae.aspx has opener or not using window.opener in javascript.
like this
In the page_load event of ChangeLanguage.aspx.cs i have to find whether it has opener or not.
how can we check whether requested page has parent or not in codebehind file.



0
 
LVL 39

Expert Comment

by:abel
ID: 24136174
> the events Application_PreSendRequestHeaders and page_load of pages.cs  
> are firing and i am able to redirect employee2 to Errorpage.aspx


good!

> i am facing problem when logged in as employee1 and immediately logged
> out and checkig the accessbility of employee1 page by logged in as employee2.


sounds to me that you have a genuine bug there. If your Employee1 logs out, do you reset the session? I.e., do you do a: Session.Clear()? I don't know how your code goes, but this is clearly not a caching issue, but a server-side coding issue. Try to walk through your code step by step (debug mode) and check why the cod thinks Employee1 is still logged in when Employee2 should be logged in.
0
 
LVL 39

Expert Comment

by:abel
ID: 24136191
> In the page_load event of ChangeLanguage.aspx.cs i have to find whether it has opener or not.> how can we check whether requested page has parent or not in codebehind file.

So far I've been helping even after the question was closed, because the questions were still somehow related. This new question is not related to the original question. Please ask it as a new question on EE in the Javascript + .NET zones.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now