Geeks With Blogs
Willem's... {rue if I mellow}

This means: forcing a page postback or redirect from a client browser without explicit user input.

Why would you want to do that? The main reasons are:

  • Auto-redirecting to a new URL after a brief time period
  • Refreshing page information periodically
  • Maintaining a valid session state indefinitely
  • Forcing a specific process to run on the server when the client session has expired.

The following methods are the constructs we have used in our web development projects:

HTML header refresh tag

The most common and best known way - a tag of the following format is placed in the HTML section of the page:

<meta http-equiv="refresh" content="8;url=http://dotnet.org.za/">

  • where '8' refers to the number of seconds that will elapse before the page is refreshed;
  • 'url' is the new url redirect to. It can be excluded which means the current page will be reloaded.

This construct is useful if you have one or two pages which have to auto-refresh, and works for any HTML content type forms.  The downside is the refresh interval cannot be set dynamically, and if you are testing for session timeouts on your site, you'll have to embed this construct in every page of the site.

Response.AppendHeader method

ASP.NET provides the AppendHeader method to the Response object. Typically the page refresh can be set as follows in an ASP.NET webform (in C#):

this.Response.AppendHeader("Refresh",
    Convert.ToString(Session.Timeout * 60 + 5));

Here the page refresh is set to 5 seconds after the client session timeout setting specified in the web.config file.

This construct is useful as it can be placed in a base webform OnLoad() or Page_Load() response method. All derived webforms will then have the same page refresh setting when they are loaded. Obviously, if the timeout value is changed in the web.config file, no modification is required in this code and everything still works fine.

Page OnLoad method script

The same thing can be done by setting a script for the client-side HTML using the HtmlGenericControl.Attributes collection:

string onload = "window.setTimeout(
    'window.location.href=window.location.href'," +
    Convert.ToString( (Session.Timeout * 60 + 5) * 1000) + ");";
this.body.Attributes.Add("onload", onload);

Unfortunately, to get this to work you need to add the following attribute to the tag in the HTML:

runat="server"

and the following member in the webform code-behind class:

protected System.Web.UI.HtmlControls.HtmlGenericControl body;

Hmmm - so why do this, if it means that we have to fiddle with the HTML?

In our experience, certain ASP.NET controls in the webform actually kills the working of the previous two constructs - but this method always seems to work....regardless...in Internet Explorer...

In future posts, I will look more closely at managing ASP.NET session timeouts with re-logins for secured sites.

Posted on Sunday, October 30, 2005 7:19 PM .NET Adventures | Back to top


Comments on this post: Auto-refreshing ASP.NET web pages

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
This is really good for autorefreshig pages but in this case page appear as submitting so in my opinion it should be in javascript then it will be better......
Left by Deepak Kumar Sharma on Mar 03, 2006 3:45 AM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
Gud one
Left by sara on Apr 01, 2006 11:24 AM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
is there an short-cut method where by this can be applied to each webform without having to add this line of code in each file?
Left by YN on Apr 05, 2006 7:17 AM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
this is vary helpful. Thank you.
Left by CFox on Jun 12, 2006 5:00 PM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
This is exactly what I needed...Thanks.
Left by lvh on Jun 16, 2006 3:53 PM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
Good One
Left by Pawan Jahagirdar on Sep 18, 2006 7:52 AM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
First Time I've seen that question answered correctly and entirely.

Thankyou...
Left by Stuart Markus on Feb 26, 2007 8:45 AM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
simple and wonderful post

thanks
m@ndar
Left by Mandar on May 16, 2007 7:14 PM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
Really it is helpful for me thanxs.
Left by Prabhash chandra on Jul 07, 2007 12:31 AM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
Its really working. Thank you
Left by jakkuboy on Nov 01, 2007 11:51 PM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
Is there a way to remove the response.appendheader method once it has been implemented?

The problem I'm having is that I have a page that refreshes by the method above. The problem is that when I do a response.redirect from this page, the destination page loads and then refreshes also (but only once). I do not want the destination page to refresh at all.

Thanks
Left by Syzygy on Dec 04, 2007 5:08 PM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
I found a work-around.

Before the redirect I used Header.Dispose() and it worked.


Thanks
Left by Syzygy on Dec 04, 2007 5:15 PM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
" I logout user using "Session.Abandon();
FormsAuthentication.SignOut();".... But the problem is user can type or use back arrow and it shows the previous page. But they can not use that page.. but still shows the page...even if I refresh the page.. Any advice.. Thanks in advace..
Left by pavel on Dec 11, 2007 1:30 AM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
Try (this is a vb example)

if session("example") is nothing then
response.redirect("signin.aspx")
end if
Left by Syzygy on Dec 13, 2007 10:02 AM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
Good Article...!
Short, simple and comprehensive...!!!

Thanks a lot.
Left by Bharat on Apr 30, 2008 4:07 AM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
Hi WebMaster!!!
Thank you very much man. Thats wonderful.
Left by Hameed on May 05, 2008 7:08 AM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
how about if i want to refresh a part of the page
Left by lingaraj on Jun 14, 2008 3:19 PM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
Thanks for writting an interesting article. You can dynamically set the refresh interval on the meta tag though by adding an id and runat:

<meta http-equiv="refresh" id="autoRefresh" runat="server" />

and then in the code-behind saying:

autoRefresh.Attributes("content") = "8;url=http://dotnet.org.za/"

Hope this helps.

Regards,

Alan
Left by Alan on Jul 11, 2008 3:59 AM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
hi

In asp.net web application how to refresh the web control without refreshing the whole page.

Thanks in advance

Raj.
Left by Raj on Jul 21, 2008 6:56 AM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
Have to say this is a refreshing article!
Left by Topcat on Jul 28, 2008 11:13 PM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
How to stop the periodic autorefresh after, for example, 5 refreshes?
Left by Ton on Aug 26, 2008 1:01 AM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
Thank you very much for solving my problem, i'm searching for this solution for ages but urs are the first correct code for auto-redirect.
Left by Magna on Aug 27, 2008 2:21 AM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
how to refresh the page without clearing the textbox content??
Left by desigan on Aug 29, 2008 6:45 AM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
Live gold prices
Left by Raufabdullatif on Sep 18, 2008 11:44 AM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
How do you make it refresh every 2 min, instead of just once after 2 min?
Left by Gsears on Oct 14, 2008 2:16 PM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
how to refresh my web page in every 1 minit how to

solved this problem.
Left by mohit kadvani on Feb 12, 2009 12:15 AM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
Thanx!!!
I was expecting that it will take me couple of hours to make webpage autorefresh. but I was able to do it in 120 seconds.

Thanx.
Left by Nirav on Feb 12, 2009 6:29 AM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
Add my thanks to the list!
Big help :)

Tony!
Left by Tony on Apr 13, 2009 4:26 PM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
good for all
Left by ynuthan on Apr 17, 2009 12:37 AM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
ALL IZZ WELL
Left by honey on Aug 18, 2010 6:05 AM

# re: Auto-refreshing ASP.NET web pages
Requesting Gravatar...
It is really a nice article.
Left by Biswasagar on Aug 02, 2011 3:11 PM

Your comment:
 (will show your gravatar)


Copyright © Willem Fourie | Powered by: GeeksWithBlogs.net | Join free