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

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.


Solution 1:

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.

Solution 2:

This might be late, but I hope it helps someone who is looking for the answer.

You can use the following line to do that:

Server.TransferRequest(Request.Url.AbsolutePath, false);

Try to avoid using Response.Redirect as it increases the steps in the process. What it actually does is:

  1. Sends back the page with redirection header
  2. The Browser redirects to the destination URL.

As you can see, the same outcome takes 2 trips rather than 1 trip.

Solution 3:

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.

Solution 4:

Try this:

Response.Redirect(Request.Url.AbsoluteUri);

Solution 5:

Use javascript's location.reload() method.

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