Tuesday, October 9, 2012

How to refresh page automatically? - 4 different ways


Some of the sites those work on the basis of updated data like - Cricket score updates, Share market data, For those kind of sites it is required that to update or refresh the page automatically. In this article, I am  explaining - How to refresh page automatically?

Here I am showing 4 different ways to update or refresh the page automatically. This can be done using following ways-

1. Client side code
2. Server side code

Client side code-

In client side we have following options for this-

A. Using meta tag-

By setting the attributes value in meta tag you can auto refresh the page.

Like this-

<head runat="server">
 <meta  http-equiv="REFRESH" content="1">
</head>
 

B. Using reload() -

reload() function refresh the page explicitly, So use this function inside  setTimeout() function and give interval for setTimeout().

Like this-

<script type="text/javascript">
        function RefreshPage(time) {
            setTimeout("location.reload(true);", time);
        }
</script>


Now call this function on body load() event.

<body onload="RefreshPage(1000);">


Here 1000 is time to refresh the page after particular interval, you can change this time.

Server side code-

In server side we have following options for this-

A. Using Response.AddHeader()

you can set value for refreshing the page in Page_Load() event.

Like this-

protected void Page_Load(object sender, EventArgs e)
{
   Response.AddHeader("Refresh", "1000");
}


B. Using Timer control.

By setting the Interval and Enabled property you can refresh the page.

Like this-

first set Enabled property of Timer control= true
now set Interval property of Timer control= 1000 (your desired value)

last  you need to implement Tick() event for Timer Control-

protected void TimerControl1_Tick(object sender, EventArgs e)
{
    //Your code
}



Now done, these are some possible ways for auto refreshing the page; you can use any of these.



Thanks


No comments:

Post a Comment