How do I refresh the page in ASP.NET? (Let it reload itself by code)

Viewed 575076

How do I refresh a page in ASP.NET? (Let it reload itself by code)

I'd rather not use Response.Redirect() because I don't know if the page I will be on, as it's inside a user control inside a webpart inside sharepoint.

14 Answers

In my user controls, after updating data I do:

  Response.Redirect(Request.RawUrl);    

That ensures that the page is reloaded, and it works fine from a user control. You use RawURL and not Request.Url.AbsoluteUri to preserve any GET parameters that may be included in the request.

You probably don't want to use: __doPostBack, since many aspx pages behave differently when doing a postback.

Once the page is rendered to the client you have only two ways of forcing a refresh. One is Javascript

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

The second is a Meta tag:

<meta http-equiv="refresh" content="600">

You can set the refresh intervals on the server side.

Try this:

Response.Redirect(Request.Url.AbsoluteUri);

Use javascript's location.reload() method.

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

If you don't want to do a full page refresh, then how about wrapping what you want to refresh inside of a UpdatePanel and then do an asynchronous postback?

You can't do that. If you use a redirect (or any other server technique) you will never send the actual page to the browser, only redirection pages.

You have to either use a meta tag or JavaScript to do this, so that you can reload the page after it has been displayed for a while:

ScriptManager.RegisterStartupScript(this, GetType(), "refresh", "window.setTimeout('window.location.reload(true);',5000);", true);

In your page_load, add this:

Response.CacheControl = "no-cache";
Response.AddHeader("Pragma", "no-cache");
Response.Expires = -1;

for asp.net core 3.1

Response.Headers.Add("Refresh", "2");// in secound

and

Response.Headers.Remove("Refresh");
Related