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>

      .csharpcode, .csharpcode pre
      {
      font-size: small;
      color: black;
      font-family: consolas, “Courier New”, courier, monospace;
      background-color: #ffffff;
      /*white-space: pre;*/
      }
      .csharpcode pre { margin: 0em; }
      .csharpcode .rem { color: #008000; }
      .csharpcode .kwrd { color: #0000ff; }
      .csharpcode .str { color: #006080; }
      .csharpcode .op { color: #0000c0; }
      .csharpcode .preproc { color: #cc6633; }
      .csharpcode .asp { background-color: #ffff00; }
      .csharpcode .html { color: #800000; }
      .csharpcode .attr { color: #ff0000; }
      .csharpcode .alt
      {
      background-color: #f4f4f4;
      width: 100%;
      margin: 0em;
      }
      .csharpcode .lnum { color: #606060; }

       

      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");
      }
      }

      .csharpcode, .csharpcode pre
      {
      font-size: small;
      color: black;
      font-family: consolas, “Courier New”, courier, monospace;
      background-color: #ffffff;
      /*white-space: pre;*/
      }
      .csharpcode pre { margin: 0em; }
      .csharpcode .rem { color: #008000; }
      .csharpcode .kwrd { color: #0000ff; }
      .csharpcode .str { color: #006080; }
      .csharpcode .op { color: #0000c0; }
      .csharpcode .preproc { color: #cc6633; }
      .csharpcode .asp { background-color: #ffff00; }
      .csharpcode .html { color: #800000; }
      .csharpcode .attr { color: #ff0000; }
      .csharpcode .alt
      {
      background-color: #f4f4f4;
      width: 100%;
      margin: 0em;
      }
      .csharpcode .lnum { color: #606060; }

      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.

      Advertisements
      This entry was posted in Tips and tagged . Bookmark the permalink.

      3 Responses to Auto-Refreshing ASP.NET Web Pages

      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.

      2. andolasoft says:

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

      Leave a Reply

      Fill in your details below or click an icon to log in:

      WordPress.com Logo

      You are commenting using your WordPress.com account. Log Out / Change )

      Twitter picture

      You are commenting using your Twitter account. Log Out / Change )

      Facebook photo

      You are commenting using your Facebook account. Log Out / Change )

      Google+ photo

      You are commenting using your Google+ account. Log Out / Change )

      Connecting to %s