Tuesday, February 1, 2011

Auto-Refreshing ASP.NET Web Pages

 

Today I am writing about how you can auto-refresh the page without user click .If you want to refresh the page at specific time of interval like after each 10 seconds you can used any of the following methods.

HTML header refresh tag

 

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

    <meta http-equiv="refresh" content=" 5; url=http://dotnetfarrukhabbas.blogspot.com/">

    • where '5' 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.

      You can used this meta tag in master page as well as any specific page.

       

      Using JavaScript

      You can also used the JavaScript for auto refreshing the page instead of meta tag.

      <script language="javascript" type="text/javascript"> 

      setTimeout("StratRefresh()", 30000);
      function StratRefresh() {
      window.location.reload();
      }
      </script>



       


      Refresh Page from Server Side


      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#):

      protected void Page_Load(object sender, EventArgs e)
      {
      if (!Page.IsPostBack)
      {
      //page will be refereshed at a interval of 10 sec
      Response.AddHeader("Refresh", "10");
      }
      }



      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.


      Note 


      when the page gets refresh then it will not persist the view-state of the page.

      2 comments:

      1. Just to add one. You can also use ajax for the same. This way you will be pinging the respected object for a time frame till the status of the item changes.

        ReplyDelete
      2. This way you will be pinging the respected object for a time frame till the status of the item changes. Is it fact?

        ReplyDelete

      PDF Arabic watermark using MVC and iTextSharp

      PDF full page Arabic watermark using MVC and iTextSharp Download :  Source Code Most of the time we have requirement to  gen...