How to set up automatic refresh on an asp.net site

I want to have a refresh that recalls the database to pick up changes.  I have timers set up on different pages that call the database. (using System.Timers.Timer() )  But if a user moves to different page that has another timer I get more than one timer calling the database and this is bad for performance. What is the standard way to handle an automatic refresh?
Thanks in advanced
pensonAsked:
Who is Participating?
 
OnthraxConnect With a Mentor Commented:
Yes as javascript is client based when the browser moves away from the page, either by closing the browser or loading another page, all javascript on that page will stop.

There is an unload even that will fire when the browser moves away from the page.
For an example check: http://www.dcs.uwaterloo.ca/~anderson/JavaScript/ex_load_unload.html
0
 
OnthraxCommented:
I'd forget about using a timer and approach it a little differently.

Set up a shared property that contains a date + time. Now every time a page request is made, check if the interval you want is passed and if so, update your stuff from the database and set the shared property to that current time.

Example:
10:00 am - page request. Check DateTime variable. If Current Time - DateTime > 10 do stuff and update DateTime variable. If not do nothing.

This does not damage performance as only one variable is maintained and a 'simple' compare is sufficient to check whether or not do take action.

Hope this helps.
0
 
pensonAuthor Commented:
How do I refresh the screen if there are no page requests?
The user logs in and just watches the screen which needs to update every 5 seconds on its own.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
OnthraxCommented:
You could use a javascript refresh which gets initiated when the page first loads and gets cancelled when the page gets closed.
0
 
pensonAuthor Commented:
But then I only get updates when the user first loads the screen right? I need the screen to keep updating on it's own every 5 seconds.
0
 
OnthraxCommented:
every 5 seconds.. wow. okay.. then i'd do it a little differently again hehe..

Put the stuff you want updated into an updatepanel.
Create a little javascript that will trigger a refresh on the updatepanel every 5 seconds.

This way when the page gets loaded, the javascript will get fired and will update the content inside your updatepanel every 5 seconds, which is exactly what you want if I'm correct now..

You can find here how to update the updatepanel through javascript:
http://www.asp.net/learn/ajax-videos/video-172.aspx

Good luck.

0
 
pensonAuthor Commented:
I will look at the link. Does the javascript timer stop when the user goes to another page? Or can I stop it with some javascript event? Is there some javascript event that fires when a user leaves the page?
0
 
pensonAuthor Commented:
thanks I will try this
0
 
pensonAuthor Commented:
I am having a difficult time getting this to work because I am using a master page. When using a master page if you call a postback from javascript I get IsInAsyncPostBack = false so it is not an ajax post back and you get a page that flickers.
0
 
OnthraxCommented:
I am using the same technique and also a masterpage and it's working fine.

Perhaps you should create a new thread for this 'new' problem. Post back the link and I'll assist you again, but this way you might get more input from other people as well to get your problem fixed a.s.a.p.
0
 
pensonAuthor Commented:
I created a new thread for this problem and posted the code for the master and child pages.
Are you using VS 2008 .net 3.5?

Ajax not working with master pages

http://www.experts-exchange.com/Programming/Languages/Scripting/AJAX/Q-23950269-Ajax-not-working-with-master-pages.html

thanks in advanced
0
 
pensonAuthor Commented:
Could you attach a simple example you used with a master page and a content page?
0
 
OnthraxCommented:
Ok. Let's leave this thread and continue in your supplemented one..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.