ASP.net Redirect to the calling page

alisabzevari picture alisabzevari · Jun 28, 2011 · Viewed 23.6k times · Source

I have a page that calls another page with some query string parameters. I want to return back to that page after clicking on a button.

I have to mention that I write that code in a user control and I don't know what page called that second page.

Is there something like Back button in browsers?

Answer

ibram picture ibram · Jun 28, 2011

Simplest way use javascript on client side with

window.back();

For server side you need to save the url referer in page_load:

if(!Page.IsPostback)
{
  ViewState["GoBackTo"] = Request.UrlReferrer;
}

and on a button click using Response.Redirect:

Response.Redirect( ViewState["GoBackTo"].ToString() );

edit: please note ppumkin's comment below!