Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can any one help me..


How To Autorefresh A Page Every Two Minutes?
Posted

Automatic web page refresh can be implemented in an ASP.NET (.aspx) web-page by adding some HTML code in the header section.
You can simply add following line of code in the Header section to enable auto refresh in an ASP.NET web page.
HTML
<meta http-equiv="refresh" content="120">

Where 120 refers to the number of seconds it will take before refreshing the page.
Also you can redirect to a specific page if you put the URL inside the tag.
HTML
<meta http-equiv="refresh" content="15;url=http://www.portinggurus.com">

In reality, this is standard HTML functionality and nothing specific to ASP.NET. The same effect of auto-refresh would be seen whether it is an ASP.NET page or just a HTML page, or a page made in Java, PHP, ColdFusion, Perl or the like.
If you want to set the refresh time dynamically then that can be done in ASP.NET by adding server side code in the Page_Load function to set it, as shown below:
HTML
Response.AppendHeader("Refresh", "120")

Get a good example with source code here:
Auto Refresh Web Page[^]
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 12-Mar-12 2:11am    
Surely, a 5.
--SA
Prasad_Kulkarni 12-Mar-12 2:32am    
:) Thank you sir,
The solution above is the simplest, but it is not the elegant one. This code would require browser to refresh everything which will make your page flashing (blinking). More professional and better-looking approach is to use two built-in controls - Update Panel and Timer.

Simply drag and drop controls you want to update onto Update Panel and you will have something like that.

ASP.NET
<asp:updatepanel id="UpdatePanel1" runat="server" xmlns:asp="#unknown">
   <contenttemplate>
      <asp:label id="UpdateLabel1" runat="server"></asp:label>
      <asp:gridview id="UpdateGridView1" runat="server"></asp:gridview>
      <asp:timer id="Timer1" runat="server"></asp:timer>
   </contenttemplate>
   <triggers>
   </triggers>
</asp:updatepanel>


Timer control is based on time intervals called Ticks, which after time specified by user (in milliseconds) will lunch Tick() event. For your purpose set Timer property Interval for 120000, which is equivalent of 120 seconds. Inside tick event write all your data assignment code, for example:

C#
protected void Timer1_Tick(object sender, EventArgs e)
{
  //read data from SQL or file 
  Label1.Text = SomeString;
  GridView1.DataSource = DataSet;
  GridView1.DataBind();
}


To make your page components autorefresh you will need to set Trigger. Click for Triggers collection, add new AsyncPostBack trigger and in Behavior section set ControlID for Timer1 and EventName for Tick.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900