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