简体   繁体   English

ASP.net 重定向到调用页面

[英]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最简单的方法是在客户端使用 javascript

window.back();

For server side you need to save the url referer in page_load:对于服务器端,您需要在 page_load 中保存 url 引用:

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

and on a button click using Response.Redirect:并使用 Response.Redirect 在按钮上单击:

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

edit : please note ppumkin's comment below!编辑:请注意下面 ppumkin 的评论!

You could look at Cross Page Posting .您可以查看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或者,如果您以编程方式生成链接,您可以在 url 中包含 returnUrl,例如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:您可以使用 Request.UrlReferrer,但不一定总是从客户端发送:

        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;");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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