Willem's...

{rue if I mellow}

  Home  |   Contact  |   Syndication    |   Login
  25 Posts | 0 Stories | 133 Comments | 52 Trackbacks

News

Archives

Post Categories

Businessware Architects

XML-FX.COM

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

Feedback

# re: Auto-refreshing ASP.NET web pages 3/3/2006 3:45 AM Deepak Kumar Sharma
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......

# re: Auto-refreshing ASP.NET web pages 4/1/2006 11:24 AM sara
Gud one

# re: Auto-refreshing ASP.NET web pages 4/5/2006 7:17 AM YN
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?

# re: Auto-refreshing ASP.NET web pages 6/12/2006 5:00 PM CFox
this is vary helpful. Thank you.

# re: Auto-refreshing ASP.NET web pages 6/16/2006 3:53 PM lvh
This is exactly what I needed...Thanks.

# re: Auto-refreshing ASP.NET web pages 9/18/2006 7:52 AM Pawan Jahagirdar
Good One

# re: Auto-refreshing ASP.NET web pages 2/26/2007 8:45 AM Stuart Markus
First Time I've seen that question answered correctly and entirely.

Thankyou...

# re: Auto-refreshing ASP.NET web pages 5/16/2007 7:14 PM Mandar
simple and wonderful post

thanks
m@ndar

# re: Auto-refreshing ASP.NET web pages 7/7/2007 12:31 AM Prabhash chandra
Really it is helpful for me thanxs.

# re: Auto-refreshing ASP.NET web pages 11/1/2007 11:51 PM jakkuboy
Its really working. Thank you

# re: Auto-refreshing ASP.NET web pages 12/4/2007 5:08 PM Syzygy
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

# re: Auto-refreshing ASP.NET web pages 12/4/2007 5:15 PM Syzygy
I found a work-around.

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


Thanks

# re: Auto-refreshing ASP.NET web pages 12/11/2007 1:30 AM pavel
" 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..

# re: Auto-refreshing ASP.NET web pages 12/13/2007 10:02 AM Syzygy
Try (this is a vb example)

if session("example") is nothing then
response.redirect("signin.aspx")
end if


# re: Auto-refreshing ASP.NET web pages 4/30/2008 4:07 AM Bharat
Good Article...!
Short, simple and comprehensive...!!!

Thanks a lot.

# re: Auto-refreshing ASP.NET web pages 5/5/2008 7:08 AM Hameed
Hi WebMaster!!!
Thank you very much man. Thats wonderful.


# re: Auto-refreshing ASP.NET web pages 6/14/2008 3:19 PM lingaraj
how about if i want to refresh a part of the page

# re: Auto-refreshing ASP.NET web pages 7/11/2008 3:59 AM Alan
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

# re: Auto-refreshing ASP.NET web pages 7/21/2008 6:56 AM Raj
hi

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

Thanks in advance

Raj.

# re: Auto-refreshing ASP.NET web pages 7/28/2008 11:13 PM Topcat
Have to say this is a refreshing article!

# re: Auto-refreshing ASP.NET web pages 8/26/2008 1:01 AM Ton
How to stop the periodic autorefresh after, for example, 5 refreshes?

# re: Auto-refreshing ASP.NET web pages 8/27/2008 2:21 AM Magna
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.

# re: Auto-refreshing ASP.NET web pages 8/29/2008 6:45 AM desigan
how to refresh the page without clearing the textbox content??

# re: Auto-refreshing ASP.NET web pages 9/18/2008 11:44 AM Raufabdullatif
Live gold prices

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

# re: Auto-refreshing ASP.NET web pages 2/12/2009 12:15 AM mohit kadvani
how to refresh my web page in every 1 minit how to

solved this problem.

# re: Auto-refreshing ASP.NET web pages 2/12/2009 6:29 AM Nirav
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.

# re: Auto-refreshing ASP.NET web pages 4/13/2009 4:26 PM Tony
Add my thanks to the list!
Big help :)

Tony!


# re: Auto-refreshing ASP.NET web pages 4/17/2009 12:37 AM ynuthan
good for all

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: