Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 357
alisabzevari Avatar asked Jun 28 '11 17:06

alisabzevari


4 Answers

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!

like image 117
ibram Avatar answered Oct 17 '22 09:10

ibram


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.

like image 30
Ben Foster Avatar answered Oct 17 '22 08:10

Ben Foster


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

        Response.Redirect(Request.UrlReferrer.AbsoluteUri);
like image 3
Robert Beaubien Avatar answered Oct 17 '22 10:10

Robert Beaubien


put this line of code on the page load event

 Btn_Back.Attributes.Add("onClick", "javascript:history.back(); return false;");
like image 1
ShahnawazHamid Avatar answered Oct 17 '22 10:10

ShahnawazHamid