55

Is there a Page.Refresh type of command to refresh a page?

I don't want to redirect to the page or refresh in JavaScript.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eric
  • 7,930
  • 17
  • 96
  • 128
  • Do you want a full page cycle? If so, the solutions provided should work. However, you said you don't want to redirect... so I'm not exactly sure what you want to happen. – Chris Feb 10 '10 at 21:03
  • Thanks Chris. Fermin nailed it. – Eric Feb 10 '10 at 21:09

11 Answers11

125

I think this should do the trick (untested):

Page.Response.Redirect(Page.Request.Url.ToString(), true);
Fermin
  • 34,961
  • 21
  • 83
  • 129
  • 1
    I know this post was a long time ago. But I have just bumped into it and I was wondering why your second parameter in "true". As far as I know, the best practice is actually "false" when using the Response.Redirect. What do you think? – aleafonso Feb 16 '12 at 09:40
  • 3
    In general "false" is better practice as "true" causes a ThreadAbortException to be thrown, which is costly. As always though it depends on your requirements. – Fermin Feb 16 '12 at 10:10
  • I almost feel bad upvoting this because it is such an easy answer! – mcfea Dec 22 '15 at 23:44
20

Careful with rewriting URLs, though. I'm using this, so it keeps URLs rewritten.

Response.Redirect(Request.RawUrl);
Bondt
  • 798
  • 9
  • 22
7
Response.Redirect(Request.Url.ToString());
Mayank Pathak
  • 3,621
  • 5
  • 39
  • 67
Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117
  • 9
    Watch out that can be used to do XSS exploits. You're blindly trusting the URL from the user. You'd be better off to do Response.Redirect( "~AbsolutePage.aspx" ); – Keith Adler Feb 10 '10 at 21:05
  • 3
    Can someone enlighten me on how this would be used to do XSS? It redirects to current page, and cannot do more than this page allows, no? – Bill Yang Jun 30 '11 at 17:09
  • This is the current page URL, not a return URL query string parameter.. I don't see the XSS in this either. – markt Jun 30 '11 at 20:19
6

You can just do a regular postback to refresh the page if you don't want to redirect. Posting back from any control will run the page lifecycle and refresh the page.

To do it from javascript, you can just call the __doPostBack() function.

womp
  • 115,835
  • 26
  • 236
  • 269
  • 3
    Wow, harsh. Pretty liberal with the downvotes there hey? He also said no redirects, and it was completely an aside to the rest of the answer. – womp Feb 10 '10 at 21:06
  • 1
    I didnt find the answer useful, so I clicked the down arrow. That's just what I do. No offense! – Josh Stodola Feb 11 '10 at 00:06
4

You shouldn't use:

Page.Response.Redirect(Page.Request.Url.ToString(), true);

because this might cause a runtime error.

A better approach is:

Page.Response.Redirect(Page.Request.Url.ToString(), false);
        Context.ApplicationInstance.CompleteRequest();
Beniamin Makal
  • 164
  • 1
  • 5
3

Use:

Response.Redirect(Request.RawUrl, true);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Syed Umar Ahmed
  • 5,612
  • 1
  • 21
  • 23
2

Depending on what exactly you require, a Server.Transfer might be a resource-cheaper alternative to Response.Redirect. More information is in Server.Transfer Vs. Response.Redirect.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tomas Vana
  • 18,317
  • 9
  • 53
  • 64
0

I use

Response.Redirect(Page.Request.Path);

If you have to check for the Request.Params when the page is refresh use below. This will not rewrite the Request.Params to the URL.

Response.Redirect(Page.Request.Path + "?Remove=1");
Dan Ng
  • 11
  • 3
0

I use # for Current page url address at Redirect to Refresh and that working currectly. What do you think about this:

Response.Redirect("#")
vblover
  • 1
  • 4
-3

Call Page_load function:

Page_Load(sender, e);

Paulo
  • 1
-5

To refresh the whole page, but it works normally:

Response.Redirect(url,bool) 
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 7
    Welcome to stack overflow! Could you explain what you mean by it working 'normal'? Details are great for people who come by and see your answer later! – tmesser Oct 26 '12 at 17:26