ASP.net Redirect to the calling page

23,808

Solution 1

You could look at Cross Page Posting.

Alternatively, if you are generating the link programatically you could include the returnUrl in the url e.g. http://localhost/secondpage.aspx?returnurl=firstpage.aspx

You can then read this querystring parameter in the secondpage and perform as redirect back once your work is done.

Solution 2

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!

Solution 3

You can use the Request.UrlReferrer, but it is not necessarily sent from the client all the time:

        Response.Redirect(Request.UrlReferrer.AbsoluteUri);

Solution 4

put this line of code on the page load event

 Btn_Back.Attributes.Add("onClick", "javascript:history.back(); return false;");
Share:
23,808
alisabzevari
Author by

alisabzevari

Programming for me is like meditation. It helps me focus, free my mind from garbage and love myself! The most interesting part of being a programmer is that you are a member of a society that are always connected to each other around the world and help each other to make the universe a better place to live. #SOreadytohelp

Updated on January 05, 2020

Comments

  • alisabzevari
    alisabzevari over 4 years

    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?

  • Aristos
    Aristos almost 13 years
    the UrlReferrer is not work all the time, how ever this is a better way than the robert.
  • Piotr Kula
    Piotr Kula over 11 years
    No it wont always work if they come direct to the page. Just catch it out like if (ViewState["GoBackTo"] == null) and if there is null just redirect to the default login page like account.aspx or similar. This is nice if you do not want to use forms authentications +1