简体   繁体   English

如何在ASP.NET中使用Javascript返回上一页

[英]How to go back to previous page using Javascript in ASP.NET

I have the following scenario: 我有以下场景:

I have the following script in my page: 我的页面中有以下脚本:

function GoBackToPreviousPage () {           
   parent.history.back();
   return false;
}

In Code Behind I have the following 在Code Behind中我有以下内容

If (some conditions are met)
  Page.ClientScript.RegisterStartupScript(this.GetType(), "GoBackToPreviousPage", "GoBackToPreviousPage();", true);

If the conditions are met I want the page to go back to the previous page, the reason I'm doing this in javascript instead of on the server is because (parent.history.back();) maintains a history of all the previous pages 如果满足条件,我希望页面返回到上一页,我在javascript中而不是在服务器上执行此操作的原因是因为(parent.history.back();)维护了以前的所有历史记录网页

I noticed that the after the RegisterStartupScript is executed and postback happens the script is executed but it stays on the same page, it does not go back to the previous page 我注意到在执行RegisterStartupScript之后发生了回发,脚本被执行但是它停留在同一页面上,它不会回到上一页

The problem is caused by the PostBack that occurs before the execution of the javascript function. 问题是由执行javascript函数之前发生的PostBack引起的。 You click the button and the current page is saved in the browser history. 单击该按钮,当前页面将保存在浏览器历史记录中。 So, when the page reloads and the script executes, you are in the same page. 因此,当页面重新加载并执行脚本时,您将处于同一页面中。 So, a not-so-pretty solution is to go back two pages in history: 因此,一个不那么漂亮的解决方案是回溯历史中的两个页面:

function GoBackToPreviousPage() {
    window.history.go(-2);
    return false;
}

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

相关问题 如何在保留上一页数据的同时返回asp.net中的上一页 - How to go back to previous page in asp.net while retaining the previous page data 在ASP.Net中打开弹出页面后,如何返回上一个窗口页面? - How to go back to previous window page after opening a pop-up page in ASP.Net? 在ASP.NET中注销后如何阻止/阻止用户返回上一页 - How to block / prevent user to go back to previous page after logout in ASP.NET 单击asp.net中的上一个按钮后,如何进入asp.net中的上一页 - how to go previous page in asp.net after clicking previous button in asp.net 如何在phonegap中使用javascript返回上一页 - How to go back to previous page using javascript in phonegap 我如何使用asp.net中的角度标签返回上一页 - How can I back to previous page using angular tab in asp.net 如何使用JavaScript将数据发送回asp.net中的调用页面 - How to send back the data to calling page in asp.net using javascript 如何防止用户返回 ASP.NET 中的上一页 - How to prevent user from going back to previous page in ASP.NET 防止退格在ASP.NET中使用JavaScript转到上一页 - Prevent backspace from going to previous page with JavaScript in ASP.NET 我的ASP.NET页如何在客户端代码和服务器代码之间来回切换几次? - How can my ASP.NET page go back and forth from client to server code and back several times?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM