IE前进和后退的实现

在项目是有时会遇到需要模拟IE前进和后退的情况,但又不能用Javascript中的History.back();来实现,当页面属于表单提交(尤其是动态加载的页面), History.back后退会造成页面过期.如果在IE中隐藏了原来的前进和后退,这是致命的,再怎么也回到不原来的页面.需要重新写前进和后退功能,以下就是我为这个写的一个例子,如有不正确地方请指教,在这里帖出来只是为了减少大家在工作中的时间.^-^

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

首先看结构:<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /> 
1.GIF

首先继承System.Web.UI.Page,在基类中添加几个属性,如下表:

LinkButton lnkBack

后退按钮

LinkButton lnkForward

前进按钮

IList<string> HistoryList

用于保存后退历史页面路径的列表,需用Session保存会话状态

IList<string> ForwardList

用于保存前进历史页面路径的列表,需用Session保存会话状态

bool IsHistory

用于判断当前请求是否为前进或后退请求.

string CurrentEventSource

取得当前请求的事件源控件名称

void OperateHistoryList()

处理前进和后退

    当后退时,取出后退列表中最后一次路径,用于跳转,并将当前页路径存入前进列表中

    当前进时,取出前进列表中最后一次路径,用于跳转,并将当前页路径存入后退列表中

 

具体处理如下图:

2.GIF

代码如下
:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
  1None.gifpublic class HistoryBasePage : System.Web.UI.Page
  2ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
  3InBlock.gif
  4ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
  5InBlock.gif        /// lnkBack 控件。
  6ExpandedSubBlockEnd.gif        /// </summary>

  7InBlock.gif        protected global::System.Web.UI.WebControls.LinkButton lnkBack;
  8InBlock.gif
  9ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 10InBlock.gif        /// lnkForward 控件。
 11ExpandedSubBlockEnd.gif        /// </summary>

 12InBlock.gif        protected global::System.Web.UI.WebControls.LinkButton lnkForward;
 13InBlock.gif
 14ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 15InBlock.gif        ///     后退历史路径列表
 16ExpandedSubBlockEnd.gif        /// </summary>

 17InBlock.gif        public IList<string> HistoryList
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 19InBlock.gif            get
 20ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 21InBlock.gif                if (Session["HistoryList"== null)
 22InBlock.gif                    return new List<string>();
 23InBlock.gif                return Session["HistoryList"as IList<string>;
 24ExpandedSubBlockEnd.gif            }

 25InBlock.gif            set
 26ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 27InBlock.gif                Session["HistoryList"= value;
 28ExpandedSubBlockEnd.gif            }

 29ExpandedSubBlockEnd.gif        }

 30InBlock.gif
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 32InBlock.gif        ///     前进历史路径列表
 33ExpandedSubBlockEnd.gif        /// </summary>

 34InBlock.gif        public IList<string> ForwardList
 35ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 36InBlock.gif            get
 37ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 38InBlock.gif                if (Session["ForwardList"== null)
 39InBlock.gif                    return new List<string>();
 40InBlock.gif                return Session["ForwardList"as IList<string>;
 41ExpandedSubBlockEnd.gif            }

 42InBlock.gif            set
 43ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 44InBlock.gif                Session["ForwardList"= value;
 45ExpandedSubBlockEnd.gif            }

 46ExpandedSubBlockEnd.gif        }

 47InBlock.gif        
 48ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 49InBlock.gif        ///     判断前一次操作是否是前进或后退
 50ExpandedSubBlockEnd.gif        /// </summary>

 51InBlock.gif        public bool IsHistory
 52ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 53InBlock.gif            get
 54ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 55InBlock.gif                if (Session["IsHistory"== null)
 56InBlock.gif                    return false;
 57InBlock.gif                return Convert.ToBoolean(Session["IsHistory"]);
 58ExpandedSubBlockEnd.gif            }

 59InBlock.gif            set
 60ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 61InBlock.gif                Session["IsHistory"= value;
 62ExpandedSubBlockEnd.gif            }

 63ExpandedSubBlockEnd.gif        }

 64InBlock.gif
 65ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 66InBlock.gif        ///     取得触发事件的控件
 67ExpandedSubBlockEnd.gif        /// </summary>

 68InBlock.gif        protected string CurrentEventSource
 69ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 70InBlock.gif            get
 71ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 72InBlock.gif                string tmp = Request.Form["__EVENTTARGET"];
 73InBlock.gif                if (tmp == null)
 74InBlock.gif                    return "";
 75InBlock.gif                return tmp;
 76ExpandedSubBlockEnd.gif            }

 77ExpandedSubBlockEnd.gif        }

 78InBlock.gif
 79InBlock.gif        protected override void OnInit(EventArgs e)
 80ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 81InBlock.gif            OperateHistoryList();
 82InBlock.gif            base.OnInit(e);
 83ExpandedSubBlockEnd.gif        }

 84InBlock.gif
 85ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 86InBlock.gif        ///     处理前进和后退
 87InBlock.gif        ///     当后退时,取出后退列表中最后一次路径,用于跳转,并将当前页路径存入前进列表中
 88InBlock.gif        ///     当前进时,取出前进列表中最后一次路径,用于跳转,并将当前页路径存入后退列表中
 89ExpandedSubBlockEnd.gif        /// </summary>

 90InBlock.gif        private void OperateHistoryList()
 91ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 92InBlock.gif            string linkUrl = string.Empty;
 93InBlock.gif            IList<string> forwardList = this.ForwardList;
 94InBlock.gif            IList<string> backList = this.HistoryList;
 95InBlock.gif            switch (this.CurrentEventSource.ToLower())
 96ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 97InBlock.gif                case "lnkback":
 98InBlock.gif                    if (backList.Count > 0)
 99ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
100InBlock.gif                        linkUrl = backList[backList.Count - 1];
101InBlock.gif                        forwardList.Add(Request.RawUrl);
102InBlock.gif                        backList.RemoveAt(backList.Count - 1);
103InBlock.gif                        this.ForwardList = forwardList;
104InBlock.gif                        this.IsHistory = true;
105InBlock.gif                        Page.Response.Redirect(linkUrl);
106ExpandedSubBlockEnd.gif                    }

107InBlock.gif                    break;
108InBlock.gif                case "lnkforward":
109InBlock.gif                    if (forwardList.Count > 0)
110ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
111InBlock.gif                        linkUrl = forwardList[forwardList.Count - 1];
112InBlock.gif                        backList.Add(Request.RawUrl);
113InBlock.gif                        forwardList.RemoveAt(forwardList.Count - 1);
114InBlock.gif                        this.HistoryList = backList;
115InBlock.gif                        this.IsHistory = true;
116InBlock.gif                        Page.Response.Redirect(linkUrl);
117ExpandedSubBlockEnd.gif                    }

118InBlock.gif                    break;
119InBlock.gif                default:
120InBlock.gif                    if (Request.UrlReferrer != null && !this.IsHistory && !Page.IsPostBack)
121ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
122InBlock.gif                        backList.Add(Request.UrlReferrer.ToString());
123InBlock.gif                        this.HistoryList = backList;
124ExpandedSubBlockEnd.gif                    }

125InBlock.gif                    if (this.IsHistory)
126InBlock.gif                        this.IsHistory = false;
127InBlock.gif                    if (this.HistoryList.Count == 0)
128InBlock.gif                        this.lnkBack.Enabled = false;
129InBlock.gif                    if (this.ForwardList.Count == 0)
130InBlock.gif                        this.lnkForward.Enabled = false;
131InBlock.gif                    break;
132ExpandedSubBlockEnd.gif            }

133ExpandedSubBlockEnd.gif        }

134InBlock.gif        
135ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
136InBlock.gif        ///     后退事件
137InBlock.gif        /// </summary>
138InBlock.gif        /// <param name="sender"></param>
139ExpandedSubBlockEnd.gif        /// <param name="e"></param>

140InBlock.gif        protected void lnkBack_Click(object sender, EventArgs e)
141ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
142ExpandedSubBlockEnd.gif        }

143InBlock.gif
144ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
145InBlock.gif        ///     前进事件
146InBlock.gif        /// </summary>
147InBlock.gif        /// <param name="sender"></param>
148ExpandedSubBlockEnd.gif        /// <param name="e"></param>

149InBlock.gif        protected void lnkForward_Click(object sender, EventArgs e)
150ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
151ExpandedSubBlockEnd.gif        }

152InBlock.gif
153InBlock.gif
154ExpandedBlockEnd.gif    }

源程序下载: HistoryTest

转载于:https://www.cnblogs.com/eric-huang/archive/2008/04/02/1134121.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值