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

2019-08-11 08:17发布

问题:

This question already has an answer here:

  • Refresh Page C# ASP.NET 10 answers

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.

回答1:

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.



回答2:

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.



回答3:

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