Reputation: 14764
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.
Upvotes: 175
Views: 621022
Reputation: 703
for asp.net core 3.1
Response.Headers.Add("Refresh", "2");// in secound
and
Response.Headers.Remove("Refresh");
Upvotes: 2
Reputation: 37440
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.
Upvotes: 422
Reputation: 17354
The only correct way that I could do page refresh was through JavaScript, many of top .NET answers failed for me.
Response.Write("<script type='text/javascript'> setTimeout('location.reload(true); ', timeout);</script>");
Put the above code in button click event or anywhere you want to force page refresh.
Upvotes: 2
Reputation: 1229
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:
As you can see, the same outcome takes 2 trips rather than 1 trip.
Upvotes: 55
Reputation: 1183
There are various method to refresh the page in asp.net like...
Java Script
function reloadPage()
{
window.location.reload()
}
Code Behind
Response.Redirect(Request.RawUrl)
Meta Tag
<meta http-equiv="refresh" content="600"></meta>
Page Redirection
Response.Redirect("~/default.aspx"); // Or whatever your page url
Upvotes: 9
Reputation: 4891
I personally need to ensure the page keeps state, so all the text boxes and other input fields retain their values. by doing meta refresh it's like a new post, IsPostBack is always false so all your controls are in the initialized state again. To retain state put this at the end of your Page_Load(). create a hidden button on the page with an event hooked up, something like butRefresh with event butRefresh_Click(...). This code sets a timer on the page to fire a postback just like a user clicked the refresh button themselves. all state and session is retained. Enjoy! (P.S. you may need to put the directive in the @Page header EnableEventValidation="false" if you receive an error on postback.
//tell the browser to post back again in 5 seconds while keeping state of all controls
ClientScript.RegisterClientScriptBlock(this.GetType(), "refresh", "<script>setTimeout(function(){ " + ClientScript.GetPostBackClientHyperlink(butRefresh, "refresh") + " },5000);</script>");
Upvotes: 4
Reputation: 73
In your page_load
, add this:
Response.CacheControl = "no-cache";
Response.AddHeader("Pragma", "no-cache");
Response.Expires = -1;
Upvotes: 3
Reputation: 21
You can use 2 ways for solve this problem: 1) After the head tag
<head>
<meta http-equiv="refresh" content="600">
</head>
2) If your page hasn't head tag you must use Javascript to implement
<script type="text/javascript">
function RefreshPage()
{
window.location.reload()
}
</script>
My contact:
Upvotes: 2
Reputation: 700152
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);
Upvotes: 3
Reputation: 21
Response.Write("<script>window.opener.location.href = window.opener.location.href </script>");
Upvotes: 2
Reputation: 4875
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.
Upvotes: 47
Reputation: 43067
Use javascript's location.reload() method.
<script type="text/javascript">
function reloadPage()
{
window.location.reload()
}
</script>
Upvotes: 14
Reputation: 27596
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?
Upvotes: 7