window.history.back()的改进方法window.history.go()

      今天在做项目时,測试人员提出了一条bug,起初没当回事,在改动过程中才意识到其重要性,故记录下来。

      依照需求,系统应该实现例如以下的功能:有三个关联的页面a.aspx(简称a),b.aspx(简称b),,c.aspx(简称c),当中a页面主要是进行因子录入,所有录入后点击“模型检验”,进入b页面,b页面中存在两个button,各自是“模型调整”和“取消”,点击“模型调整”进入c页面,点击“取消”返回录有数据的a页面。c页面存在两个button,各自是“模型确认”和“取消”。点击“取消”,返回到b页面。

      而bug的大概意思就是:先在b页面点击“模型调整”进入c页面,再在c页面点击“取消”,返回到b页面。最后,在b页面点击“取消”,理论上应该直接返回到录有数据的a页面,但此时却须要点击两次“取消”才干够实现。

      细致看页面,前台写的方法是window.history.back(),若要这样的方法实现理论上的效果,仅仅能是在b页面存在一个“取消”button,或者仅仅点击“取消”button。否则就会出现bug中的问题。

      我在网上找答案,可是其给出的答案不可以直接应用,我做了一些改动,终于可以实现预期效果,代码例如以下:

 

      前台,<a href="#" id="alink" runat="server"><img src="qx.jpg" border="0"></a>

 

      后台,在page_load()中写入例如以下代码:

 

            string sKey = Request.Url.LocalPath;

            HttpCookie cookfrom = Request.Cookies[sKey];

 

            if (cookfrom == null)
            {
                cookfrom = new HttpCookie(sKey);
            }

 

            string sTimes = "0 ";

            if (IsPostBack)
            {
                sTimes = cookfrom["count "];
                sTimes = (Convert.ToInt32(sTimes) + 1).ToString();
            }
            cookfrom["count "] = sTimes;
            Response.Cookies.Add(cookfrom);

            int iTimes = (GetPageRequestTimes()) * -1;
            this.alink.Attributes.Add("onclick ", "javascript:window.history.go( " + iTimes.ToString() + "); ");

 

       当中,GetPageRequestTimes()方法例如以下:

 

        public static int GetPageRequestTimes()
        {
            string sKey = System.Web.HttpContext.Current.Request.Url.LocalPath;
            HttpCookie cookfrom = System.Web.HttpContext.Current.Response.Cookies[sKey];

           

            string sTimes = cookfrom["count "];

            if (sTimes.Length > 0)

            {
                    return Convert.ToInt32(sTimes) + 1;

            }
            else

            {
                    return 0;

             }
        }

 

      在网上,另一种方法例如以下,只是看跟帖的人说“假设直接右键刷新就不起作用了”,我个人在改动时并没有採用这样的方法,所以并不太了解,仅仅是作为一种參考放在这里,以免丢失。代码例如以下:

 

      假设button的ID是LinkButton1
      protected   void   Page_Load(object   sender,   EventArgs   e)
      {
             int   x=0;
             if   (ViewState[ "x "]!=null)
             {
                   x=(int)ViewState[ "x "];
             }
             x++;
             ViewState[ "x "]=x;
 
             this.LinkButton1.Attributes.Add( "onclick ",    "window.history.go(- "+x+ ");   return   false ");

      } 

 

      注意:无论是window.go()还是window.back(),都仅仅能适用于父页面与子页面在同一个框架里,即子页面不是单独弹出的。

posted @ 2014-10-04 10:04  mfrbuaa  阅读(3987)  评论(0编辑  收藏  举报