简体   繁体   中英

ASP.net Redirect to the calling page

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?

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!

You could look at Cross Page Posting .

Alternatively, if you are generating the link programatically you could include the returnUrl in the url eg 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.

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

        Response.Redirect(Request.UrlReferrer.AbsoluteUri);

put this line of code on the page load event

 Btn_Back.Attributes.Add("onClick", "javascript:history.back(); return false;");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM