Wrox Programmer Forums
|
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 10th, 2009, 02:24 AM
Authorized User
 
Join Date: Dec 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Application context

Hi,

I have a MENU user control being referenced by all the web pages in the applicaiton. The menu control is dynamic and menu items are extracted from the database table. In this process, for each web page to load the menu control, database is hit.

I would like to store the menu items from teh database in an APPLICATION CONTEXT at the start itself.

Any suggestions as how can i accomplish this?

Thanks
 
Old December 10th, 2009, 10:35 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Why not use the Cache?

http://msdn.microsoft.com/en-us/library/6hbbsfk6.aspx
http://www.4guysfromrolla.com/articles/100902-1.aspx

hth
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
 
Old December 10th, 2009, 10:45 AM
Authorized User
 
Join Date: Dec 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Application context in asp.net web application

Thanks a lot.

Can u please elaborate a bit and let me know how and through which page i can implement this? Will this be a part of global.asax file?

Please let me know about it.

thanks.
 
Old December 10th, 2009, 10:59 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

You could certainly put it there, although I favor putting things like this on my MasterPages. If you do attempt to handle this in the global.asax I would probably handle it in the Session_Start event. Then on every page you would need to see if the item is still in Cache and, if it is, pouplate your menu with the Cache data otherwise make your database call.

The cache has to expiration modes: absolute and sliding. In the case of sliding the max time limit is 10 mins where as an absolute expiration can be anything really. This example shows you how to work with Caching in some detail:

http://msdn.microsoft.com/en-us/library/ms972379.aspx

The example is in VB.NET but it is trivial to convert it to C#.

hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
 
Old December 10th, 2009, 11:18 AM
Authorized User
 
Join Date: Dec 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Application context in asp.net web application

I have used <%@ OutputCache %> directive. How is it related to caching? Also i have a user control called: Menu (ID: mnuMenu). That men items and sub items are being populated through the database. I want this to happen only once.

So should i be writing a code in Session_Start event of Global.asax. (I am not using the master pages).

Can you provide some small code snippet for better idea.

Thanks
 
Old December 10th, 2009, 11:37 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

<%@ OutputCache %> is a page level directive. It will cache the contents of X page for a specificed amount of time. This is typically done to reduce the number of calls to a database or on a page that does not change or changes very slowly (a company's About Us page would be a good example of a page that changes very slowly or not at all).


Since you are using a user control, I might do something like this in the user control:

csharp Code:
protected void Page_Load(object sender, EventArgs e)
{
   if(!Page.IsPostBack) LoadMenu();
}
 
private void LoadMenu()
{
   if(Cache["Menu"] == null) LoadData();
 
   Menu.Data = Cache["Menu"];
}
 
private void LoadData()
{
   //Database Operation
   Cache.Insert("Menu", <your data>, null, DateTime.Now.AddDays(1), TimeSpan.Zero);
}

The over all flow is straightforward so I am not going to explain it. The "magic" happens here: Cache.Insert("Menu", <your data>, null, DateTime.Now.AddDays(1), TimeSpan.Zero); This inserts your Menu data into the cache for one day at which time it will expire and another database call will be made. You do not need to use the global.asax at all.

Now, there is a trade off. You are reducing calls to your Database server but you are putting extra load on the Webserver. Inserting stuff into Cache consumes server memory so it is not wise to insert LARGE amounts of data into the cache. In most normal uses you wont notice an issue or with performance on the server but be aware that this *could* impact server preformance.

hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
 
Old December 10th, 2009, 11:58 AM
Authorized User
 
Join Date: Dec 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Application context in asp.net web application

What does Cache["Menu"] means here? I mean what does "Menu" stands for? is it some kind of a session variable or where is the value fr it being stored?

Thanks
 
Old December 10th, 2009, 12:08 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

It is a Key. The Cache is nothing more than Key/Value pairs. In this line:

Cache.Insert("Menu", <your data>, null, DateTime.Now.AddDays(1), TimeSpan.Zero);

The first parameter, "Menu", sets the key for the item in the cache. If I change "Menu" in the above line to "MySuperAwesomeMenu" then all the instances of Cache["Menu"] would need to be replaced with Cache["MySuperAwesomeMenu"].

hth
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
The Following User Says Thank You to dparsons For This Useful Post:
irProject (December 10th, 2009)
 
Old December 10th, 2009, 02:21 PM
Authorized User
 
Join Date: Dec 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Application context in asp.net web application

I still didnt get wheere we provide the value for "Menu". where is the name value pair defined?

Also my menu also has submenu's. the menu looks like:

Home
Import
Export
Reports
Report1
Report2
Logout

the code piece u sent refers to some key value pairs. but where do we define the value for that? also how do we check fr Menu.Data that u have mentioned. :(

Please help.
 
Old December 11th, 2009, 10:51 AM
Authorized User
 
Join Date: Dec 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Application context in asp.net web application

Is there any way we can cache the menu items at an application level rather than in cache. Since cache is session/user specific, we want to store the menu at the application level.

any suggestion please?

Thanks





Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I map multiple context roots to a single web application? massterb J2EE 1 November 29th, 2009 01:47 AM
how to set context in web application [PLEASE HELP] arashdx J2EE 3 January 27th, 2009 03:53 AM
Application Context in C#.net dineshpdn Visual Studio 2005 0 October 10th, 2007 06:01 AM
A Chat Application using Context and Sessions kotaiah J2EE 0 October 10th, 2006 09:56 PM
How to create context help in web application? Alexds ASP.NET 2.0 Basics 1 February 1st, 2006 06:23 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.