54519

How can i refresh a asp page from code behind? C# [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/2240287/refresh-page-c-sharp-asp-net" dir="ltr" rel="nofollow">Refresh Page C# ASP.NET</a> <span class="question-originals-answer-count"> 9 answers </span> </li> </ul>

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:

<ol><li>I'm using Framework .NET 3.5</li> <li>I tried with this <a href="https://stackoverflow.com/questions/2240287/refresh-page-c-sharp-asp-net" rel="nofollow">post</a> but it doesn't work.</li> <li>Thanks for answer.</li> </ol>

Answer1:

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 <a href="https://msdn.microsoft.com/en-us/library/cc295400.aspx" rel="nofollow">here</a>.

Answer2:

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 <a href="https://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx" rel="nofollow">ASP.NET page lifecycle</a> 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 <a href="https://stackoverflow.com/questions/2787679" rel="nofollow">How to reload page every 5 second</a>?

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 <a href="https://stackoverflow.com/questions/25829343" rel="nofollow">How to implement real time data for a web page</a>, which discusses an UpdatePanel, AJAX polling, and using SignalR. Note I specifically recommend against UpdatePanel. They're tricky and inefficient.

Answer3:

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.

<ul><li>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</li> </ul>

Recommend

  • App.config only for my developer machine
  • Append array values to another array by keys comparison
  • Using uploadify with ashx handler
  • How to create instance variables explicitly?
  • Page EventValidation is True error?
  • Removing scripts added by ClientScript.RegisterStartupScript
  • How do I prevent HTML encoding of attributes in the code behind?
  • JavaScript function is not working
  • Use Mockito-core for create mock of final class?
  • F# comparing discriminated unions' by case identifier
  • Why String.ToString()? [duplicate]
  • Task.IsCancelled doesn't work
  • How to write string.Contains(someText) in expression Tree
  • DependencyObject.AssociatedObject is always null
  • when does setTimeout start executing in a inline
  • How do I register classes by both interface and namespace with Windsor?
  • DomPDF {PAGE_NUM} not on first page
  • Javascript simulate pressing enter in input box
  • How to add date and time under each post in guestbook in google app engine
  • SSO with signing and signature validation doesn't work
  • How to show dropdown in excel using jrxml (jasper api)?
  • Importing jscolor library in angular 2
  • Release, debug version and Authorization Google?
  • Websockets service method fails during R startup
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • How to get next/previous record number?
  • Apache 2.4 - remove | delete | uninstall
  • A cron job substitute?
  • Proper way to use connect-multiparty with express.js?
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Trying to get generic when generic is not available
  • How do I configure my settings file to work with unit tests?
  • embed rChart in Markdown
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal