7

How to refresh page using c# in every five minutes in ASP.NET?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kamal Deepak
  • 221
  • 1
  • 3
  • 9

5 Answers5

7

One is Javascript:

setTimeout("location.reload(true);", timeout);

The second is a Meta tag:

<meta http-equiv="refresh" content="300">
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Rejeesh
  • 1,236
  • 12
  • 15
3

Using the following HTML meta tag in your header <META HTTP-EQUIV="REFRESH" CONTENT="300"> should do the trick

Jagmag
  • 10,283
  • 1
  • 34
  • 58
  • Yes,This is oneway, but i want from c# – Kamal Deepak Aug 31 '12 at 06:45
  • 2
    Once your page is downloaded to the client, the server request is complete. There is no C# executing on the client and since the client has to initiate the refresh, AFAIK - you can only do it with HTML / Javascript. – Jagmag Aug 31 '12 at 06:49
  • why? For the server, where the C# is run, to initiate the refresh it would need to maintain a socket (ala HTML5 Web Sockets or long polling/Comet). If it's just a page refresh, those techniques are over-engineering IMHO. – Jim O'Neil Aug 31 '12 at 06:52
  • Response.AddHeder("Refresh","5"); we can do like this also , i am not sure this correct – Kamal Deepak Aug 31 '12 at 06:52
2

You can't force an HTML page to refresh from the server side. The client must request the page.

The only ways to do this always involve either using the META refresh tag, the Refresh HTTP header, or else javascript which forces a page reload on an interval.

Any "server-side" solution will do it by either writing javascript or the META tag to the page. There's simply no other way to do it.

dodexahedron
  • 4,584
  • 1
  • 25
  • 37
  • Response.AddHeder("Refresh","5"); we can do like this also , i am not sure this correct – Kamal Deepak Aug 31 '12 at 06:54
  • Almost. Refresh is given in seconds, so that should be 300. It's the same effect as the meta tag, but now the server has to inject it into the http response instead of just barfing out the text in the aspx file. Added mention of that header to my answer. – dodexahedron Aug 31 '12 at 06:57
0

the simplest way is

<Head>
<meta equiv="refresh" content="5">
</Head> 

or use timer control to refresh a webpage for every five minutes for example: drag and drop timer control in form.aspx and in form load add the code like below

<asp:Timer ID="Timer1" runat="server" Interval="6000" ontick="Timer1_Tick" />

form load

public void DoMagic()
{


}
protected void Timer1_Tick(object sender, EventArgs e)
{
DoMagic();
Label1.Text = "";
}
Hassanation
  • 866
  • 2
  • 14
  • 29
  • ASP.NET timers are not that simple. They cause a postback. That means a bunch of javascript and (likely) some side-effects that the OP is probably not expecting or wanting. – dodexahedron Aug 31 '12 at 06:54
  • i think it could be used within Ajax or even set ispostback to fix it – Hassanation Aug 31 '12 at 07:11
  • 1
    IsPostback will be true, yes. It's still a lot more complex than the other ways suggested here, and requires being aware of the postback state of the page. – dodexahedron Aug 31 '12 at 07:21
0
 window.setInterval(function () {
            // this will execute every 1 second

            methodCallOrAction();

        }, 1000);



function methodCallOrAction()
{
// u can call an url or do something here
}
Dhanasekar Murugesan
  • 3,141
  • 1
  • 18
  • 21