問題描述:我們有兩個頁面A和B,當我們從A跳到B后不做作任何讓頁面回傳的操作,用JS:history.go(-1)就可以回到A頁面,但是比如我們有Click,Change事件等激發了頁面的回傳,此時用history.go(-1)就回不到A頁面了。
解決的方法:我們要想辦法記錄到頁面回傳的次數N,然后用history.go(-n),就可以回到A頁面。
在B頁面中放一個控件記錄其回傳的次數,初始值為1
View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title></title> <script> function goto() { var n=document.getElementById("TextBox1").value; var n=Number(n); history.go(-n); } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> <input id="Reset1" type="button" value="reset" onclick="goto()"/> <asp:TextBox ID="TextBox1" runat="server" ToolTip="放一個控件保存頁面回傳次數">1</asp:TextBox> </div> </form> </body> </html>
在B頁面的CS代碼如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.TextBox1.Text = "1";
}
else
{
this.TextBox1.Text = Convert.ToString(Convert.ToInt16(this.TextBox1.Text) + 1);
}
}
這樣子不管你從A到了B頁面,在B頁面中做了什么操作,頁面回發了多少次,當你點擊【返回】時,都可以跳回頁面A了
