IE前进和后退的实现
在项目是有时会遇到需要模拟IE前进和后退的情况,但又不能用Javascript中的History.back();来实现,当页面属于表单提交(尤其是动态加载的页面)时, History.back后退会造成页面过期.如果在IE中隐藏了原来的前进和后退,这是致命的,再怎么也回到不原来的页面.需要重新写前进和后退功能,以下就是我为这个写的一个例子,如有不正确地方请指教,在这里帖出来只是为了减少大家在工作中的时间.^-^
首先看结构:
首先继承System.Web.UI.Page,在基类中添加几个属性,如下表:
LinkButton lnkBack |
后退按钮 |
LinkButton lnkForward |
前进按钮 |
IList<string> HistoryList |
用于保存后退历史页面路径的列表,需用Session保存会话状态 |
IList<string> ForwardList |
用于保存前进历史页面路径的列表,需用Session保存会话状态 |
bool IsHistory |
用于判断当前请求是否为前进或后退请求. |
string CurrentEventSource |
取得当前请求的事件源控件名称 |
void OperateHistoryList() |
处理前进和后退 当后退时,取出后退列表中最后一次路径,用于跳转,并将当前页路径存入前进列表中 当前进时,取出前进列表中最后一次路径,用于跳转,并将当前页路径存入后退列表中 |
具体处理如下图:
代码如下:
Code
1public class HistoryBasePage : System.Web.UI.Page
2 {
3
4 /**//// <summary>
5 /// lnkBack 控件。
6 /// </summary>
7 protected global::System.Web.UI.WebControls.LinkButton lnkBack;
8
9 /**//// <summary>
10 /// lnkForward 控件。
11 /// </summary>
12 protected global::System.Web.UI.WebControls.LinkButton lnkForward;
13
14 /**//// <summary>
15 /// 后退历史路径列表
16 /// </summary>
17 public IList<string> HistoryList
18 {
19 get
20 {
21 if (Session["HistoryList"] == null)
22 return new List<string>();
23 return Session["HistoryList"] as IList<string>;
24 }
25 set
26 {
27 Session["HistoryList"] = value;
28 }
29 }
30
31 /**//// <summary>
32 /// 前进历史路径列表
33 /// </summary>
34 public IList<string> ForwardList
35 {
36 get
37 {
38 if (Session["ForwardList"] == null)
39 return new List<string>();
40 return Session["ForwardList"] as IList<string>;
41 }
42 set
43 {
44 Session["ForwardList"] = value;
45 }
46 }
47
48 /**//// <summary>
49 /// 判断前一次操作是否是前进或后退
50 /// </summary>
51 public bool IsHistory
52 {
53 get
54 {
55 if (Session["IsHistory"] == null)
56 return false;
57 return Convert.ToBoolean(Session["IsHistory"]);
58 }
59 set
60 {
61 Session["IsHistory"] = value;
62 }
63 }
64
65 /**//// <summary>
66 /// 取得触发事件的控件
67 /// </summary>
68 protected string CurrentEventSource
69 {
70 get
71 {
72 string tmp = Request.Form["__EVENTTARGET"];
73 if (tmp == null)
74 return "";
75 return tmp;
76 }
77 }
78
79 protected override void OnInit(EventArgs e)
80 {
81 OperateHistoryList();
82 base.OnInit(e);
83 }
84
85 /**//// <summary>
86 /// 处理前进和后退
87 /// 当后退时,取出后退列表中最后一次路径,用于跳转,并将当前页路径存入前进列表中
88 /// 当前进时,取出前进列表中最后一次路径,用于跳转,并将当前页路径存入后退列表中
89 /// </summary>
90 private void OperateHistoryList()
91 {
92 string linkUrl = string.Empty;
93 IList<string> forwardList = this.ForwardList;
94 IList<string> backList = this.HistoryList;
95 switch (this.CurrentEventSource.ToLower())
96 {
97 case "lnkback":
98 if (backList.Count > 0)
99 {
100 linkUrl = backList[backList.Count - 1];
101 forwardList.Add(Request.RawUrl);
102 backList.RemoveAt(backList.Count - 1);
103 this.ForwardList = forwardList;
104 this.IsHistory = true;
105 Page.Response.Redirect(linkUrl);
106 }
107 break;
108 case "lnkforward":
109 if (forwardList.Count > 0)
110 {
111 linkUrl = forwardList[forwardList.Count - 1];
112 backList.Add(Request.RawUrl);
113 forwardList.RemoveAt(forwardList.Count - 1);
114 this.HistoryList = backList;
115 this.IsHistory = true;
116 Page.Response.Redirect(linkUrl);
117 }
118 break;
119 default:
120 if (Request.UrlReferrer != null && !this.IsHistory && !Page.IsPostBack)
121 {
122 backList.Add(Request.UrlReferrer.ToString());
123 this.HistoryList = backList;
124 }
125 if (this.IsHistory)
126 this.IsHistory = false;
127 if (this.HistoryList.Count == 0)
128 this.lnkBack.Enabled = false;
129 if (this.ForwardList.Count == 0)
130 this.lnkForward.Enabled = false;
131 break;
132 }
133 }
134
135 /**//// <summary>
136 /// 后退事件
137 /// </summary>
138 /// <param name="sender"></param>
139 /// <param name="e"></param>
140 protected void lnkBack_Click(object sender, EventArgs e)
141 {
142 }
143
144 /**//// <summary>
145 /// 前进事件
146 /// </summary>
147 /// <param name="sender"></param>
148 /// <param name="e"></param>
149 protected void lnkForward_Click(object sender, EventArgs e)
150 {
151 }
152
153
154 }
1public class HistoryBasePage : System.Web.UI.Page
2 {
3
4 /**//// <summary>
5 /// lnkBack 控件。
6 /// </summary>
7 protected global::System.Web.UI.WebControls.LinkButton lnkBack;
8
9 /**//// <summary>
10 /// lnkForward 控件。
11 /// </summary>
12 protected global::System.Web.UI.WebControls.LinkButton lnkForward;
13
14 /**//// <summary>
15 /// 后退历史路径列表
16 /// </summary>
17 public IList<string> HistoryList
18 {
19 get
20 {
21 if (Session["HistoryList"] == null)
22 return new List<string>();
23 return Session["HistoryList"] as IList<string>;
24 }
25 set
26 {
27 Session["HistoryList"] = value;
28 }
29 }
30
31 /**//// <summary>
32 /// 前进历史路径列表
33 /// </summary>
34 public IList<string> ForwardList
35 {
36 get
37 {
38 if (Session["ForwardList"] == null)
39 return new List<string>();
40 return Session["ForwardList"] as IList<string>;
41 }
42 set
43 {
44 Session["ForwardList"] = value;
45 }
46 }
47
48 /**//// <summary>
49 /// 判断前一次操作是否是前进或后退
50 /// </summary>
51 public bool IsHistory
52 {
53 get
54 {
55 if (Session["IsHistory"] == null)
56 return false;
57 return Convert.ToBoolean(Session["IsHistory"]);
58 }
59 set
60 {
61 Session["IsHistory"] = value;
62 }
63 }
64
65 /**//// <summary>
66 /// 取得触发事件的控件
67 /// </summary>
68 protected string CurrentEventSource
69 {
70 get
71 {
72 string tmp = Request.Form["__EVENTTARGET"];
73 if (tmp == null)
74 return "";
75 return tmp;
76 }
77 }
78
79 protected override void OnInit(EventArgs e)
80 {
81 OperateHistoryList();
82 base.OnInit(e);
83 }
84
85 /**//// <summary>
86 /// 处理前进和后退
87 /// 当后退时,取出后退列表中最后一次路径,用于跳转,并将当前页路径存入前进列表中
88 /// 当前进时,取出前进列表中最后一次路径,用于跳转,并将当前页路径存入后退列表中
89 /// </summary>
90 private void OperateHistoryList()
91 {
92 string linkUrl = string.Empty;
93 IList<string> forwardList = this.ForwardList;
94 IList<string> backList = this.HistoryList;
95 switch (this.CurrentEventSource.ToLower())
96 {
97 case "lnkback":
98 if (backList.Count > 0)
99 {
100 linkUrl = backList[backList.Count - 1];
101 forwardList.Add(Request.RawUrl);
102 backList.RemoveAt(backList.Count - 1);
103 this.ForwardList = forwardList;
104 this.IsHistory = true;
105 Page.Response.Redirect(linkUrl);
106 }
107 break;
108 case "lnkforward":
109 if (forwardList.Count > 0)
110 {
111 linkUrl = forwardList[forwardList.Count - 1];
112 backList.Add(Request.RawUrl);
113 forwardList.RemoveAt(forwardList.Count - 1);
114 this.HistoryList = backList;
115 this.IsHistory = true;
116 Page.Response.Redirect(linkUrl);
117 }
118 break;
119 default:
120 if (Request.UrlReferrer != null && !this.IsHistory && !Page.IsPostBack)
121 {
122 backList.Add(Request.UrlReferrer.ToString());
123 this.HistoryList = backList;
124 }
125 if (this.IsHistory)
126 this.IsHistory = false;
127 if (this.HistoryList.Count == 0)
128 this.lnkBack.Enabled = false;
129 if (this.ForwardList.Count == 0)
130 this.lnkForward.Enabled = false;
131 break;
132 }
133 }
134
135 /**//// <summary>
136 /// 后退事件
137 /// </summary>
138 /// <param name="sender"></param>
139 /// <param name="e"></param>
140 protected void lnkBack_Click(object sender, EventArgs e)
141 {
142 }
143
144 /**//// <summary>
145 /// 前进事件
146 /// </summary>
147 /// <param name="sender"></param>
148 /// <param name="e"></param>
149 protected void lnkForward_Click(object sender, EventArgs e)
150 {
151 }
152
153
154 }
源程序下载: HistoryTest