1

I'm kind of new at this and my English is also poor. Anyway:

I'm trying to set a Timer that refreshes my page if there is any new record on my report. Apparently my timer works fine because when i debugged it it enters into the function Timer1_Tick, but it doesn't refresh my Page.

Here is the code:

System.Timers.Timer Timer1 = new System.Timers.Timer();
Timer1.Interval = 10000;
Timer1.Elapsed += Timer1_Tick;
Timer1.Enabled = true;

and

protected void Timer1_Tick(object sender, EventArgs e){
  Response.Redirect("ReporteIncidencia.aspx"); //1st attempt
  ScriptManager.RegisterStartupScript(Page, typeof(Page), "somekey", "RefreshPage()", true); //2nd attempt
  Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "RefreshPage()", true); //3rd attempt
}

also

<script type="text/javascript">
function RefreshPage()
{
  window.location.reload()
}
</script>

EDIT:

  1. I'm using Framework .NET 3.5
  2. I tried with this post but it doesn't work.
  3. Thanks for answer.
Community
  • 1
  • 1
Maverick.pe
  • 1,917
  • 4
  • 15
  • 20
  • have you considered using SignalR in your application? – rajeemcariazo Feb 06 '15 at 15:51
  • 1
    @rajeemcariazo Im not acquainted with SignalR, ill take a look thanks – Maverick.pe Feb 06 '15 at 16:02
  • @Izzy i already tried with that post but nothing works, thank you – Maverick.pe Feb 06 '15 at 16:02
  • "Nothing works." <- Not useful. What happens? What does the rest of your code or environment look like? Help us help you. If you have specific environment needs, you can reform the question to include those and become a unique question. – JasonMArcher Feb 06 '15 at 19:09
  • @JasonMArcher hi, *Izzy said that i should look this post (http://stackoverflow.com/questions/2240287/refresh-page-c-sharp-asp-net) but before i posted my question here, i've already try the answers on that post. That's why i said: "nothing (of it) works". I'll reform my question and post some aditional code. Thanks for reply =) – Maverick.pe Feb 06 '15 at 22:22

3 Answers3

3

I recommend you to use Ajax to perform this operation.

But a simple way to achieve that is using Asp.Net Timer and Update Panel components.

In .aspx:

<asp:ScriptManager runat="server" id="ScriptManager1"/>
<asp:UpdatePanel runat="server" id="UpdatePanel1">
<ContentTemplate>
<asp:Timer runat="server" id="Timer1" Interval="10000" OnTick="Timer1_Tick">
</asp:Timer>
<asp:Label runat="server" Text="Page not refreshed yet." id="Label1">
</asp:Label>
</ContentTemplate>
</asp:UpdatePanel>

In code behind:

protected void Timer1_Tick(object sender, EventArgs e)
{
    Label1.Text = "Panel refreshed at: " +
    DateTime.Now.ToLongTimeString();
}

The asp:ScriptManager component is necessary to use Update Panels. More info here.

Arthur Castro
  • 610
  • 6
  • 18
0

The reason this doesn't work is that once the response is sent from the server to the client, the server can no longer modify the response, thus it can't register a startup script. You must keep in mind the ASP.NET page lifecycle and the distinction between server and client side.

Instead, you could implement a timer that runs on the client side in JavaScript, as described in How to reload page every 5 second?

However, that's not ideal, because now your page is going to request the entire HTML for the page again. That's a lot of overhead. Instead, you should refresh only the data needed. I describe various techniques for this in How to implement real time data for a web page, which discusses an UpdatePanel, AJAX polling, and using SignalR. Note I specifically recommend against UpdatePanel. They're tricky and inefficient.

Community
  • 1
  • 1
mason
  • 31,774
  • 10
  • 77
  • 121
  • I'd loved your answer! I can reload my page every interval of time elapsed with this code on initialize(): ScriptManager.RegisterStartupScript(this, GetType(), "refresh", "window.setTimeout('window.location.reload(true);',20000);", true); But i need to reload the page if something happens on code behind =(. I'm working under my company's framework so it gaves me methods to build the datagridview, anyway i'll take a look on your post. Thanks again. – Maverick.pe Feb 06 '15 at 17:04
  • @Maverick.pe The code behind ceases to play in any role in the page after the response is sent, so it can't call client side code. You need to rethink your strategy using one of the real time data techniques I discussed. – mason Feb 06 '15 at 17:17
  • Yeap .. im trying with the other real time data techniques you discussed. I tried UpdatePanel and Ajax polling but it doesnt work. I'll try with SignalR =) Thanks. – Maverick.pe Feb 06 '15 at 17:45
  • @Maverick.pe All the techniques that I described *will* work, if you configure them properly. It's more a matter of picking the best technique for your situation, then implementing it correctly. – mason Feb 06 '15 at 17:46
0

After asp.net serves the page, it ceases communication with the server. You can't push notifications from code behind to client after the page finishes loading. *

If you just want to redirect the user after some time, you check the time on the client, like:

C#:

Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "RefreshPage()", true);

JavaScript

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

Otherwise, you can create a request from client to server from time to time.

  • There are websockets and SSE, for example, but they are not the best solution if you just want to use it to refresh a page
Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Ortiga
  • 8,455
  • 5
  • 42
  • 71
  • But this will refresh my page from time to time, i just need to reload if something happens on code behind =( Thank you. If i use this code on initialize(): ScriptManager.RegisterStartupScript(this, GetType(), "refresh", "window.setTimeout('window.location.reload(true);',20000);", true); its works but if i tried to use it on Timer1_Tick .. it doesn't work. Sigh* – Maverick.pe Feb 06 '15 at 16:19
  • @Maverick.pe then you must make a request to server, like Arthur Castro"s answer. You can't send anything to the client after the page finishes loading. Unless you use SignalR (or it's underlying technologies, like websockets, SSE, etc), the connection between client and server is closed. – Ortiga Feb 06 '15 at 16:50
  • i'll search about SignalR, I never heard about it. Thank you =) – Maverick.pe Feb 06 '15 at 17:16