Click here to Skip to main content
15,068,879 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am building an ASP.NET Application which has 2 projects. One is a class library having BL Code. I want to create a public class instance variable from one of the classes in BL. This class instance variable is to avoid loading all the data on each request which makes my application to respond slow for each request. The global class variable will load data at page_Load, and remain until the user redirects to another page.
Posted
Comments
   
There is not such concept as "global variable". And using a static variable is just a bad idea in general programming, in ASP.NET, it won't help you at all.
—SA
F-ES Sitecore 21-Mar-15 7:43am
   
You said the data will exist until the user uses another page, but that you don't want it loading on each request. There is only one load per request though. Are you looking to use ajax or something on the page and have those ajax calls access data that you don't want to load each time?
Yesudass Moses 22-Mar-15 1:49am
   
yes @F-ES
I am using AJAX Update Panel.

1 solution

No, it won't. a web application is not like a Windows application: it doesn't run all the time waiting for the user to do something.

Page_Load occurs every time that the user does anything which "talk" to the server: it doesn't occur once and then never again until the user changes pages. Every time the user presses a button, Page_load is called.

And unless you preserve your information in cookies or the Session, it can and will be discarded whenever the server feels like it.

See here for starters: MSDN: ASP.NET Page Life Cycle Overview[^]
And when you have grasped that, start looking at your BL code and seeing how much of that you need to do each time the user wants to do something. Most times, if your app is slow, it's because you are not "thinking right" and have loaded everything instead of considering what the user actually needs.
   
Comments
Yesudass Moses 22-Mar-15 1:50am
   
This makes sense... Thanks
Yesudass Moses 22-Mar-15 1:51am
   
If I use AJAX Update Panel. The scenario is same ?
OriginalGriff 22-Mar-15 4:38am
   
Yes - Ajax is just a way to sending and updating a small part of a page without needing a full reload - it's not a replacement for the way the web server works!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900