开发中经常遇到页面返回的问题,可以有很多种处理方法,我在这里讲一种通用的,简便方法,可以在刷新页面后返回依然有效
//基类
/// <summary>
/// 记录提交页面次数,用在javascript:history.go(<%=GoNum%>)
/// </summary>
public int GoNum
{
get
{
if (this.ViewState["GoNum"] == null)
{
this.ViewState["GoNum"] = "-1";
return -1;
}
else
{
return Convert.ToInt32(this.ViewState["GoNum"]);
}
}
set
{
this.ViewState["GoNum"] = value.ToString();
}
}
/// <summary>
/// 更新数据后返回页数加1
/// </summary>
public void IncreaseGoNum()
{
GoNum += -1;
}
//父页面Page_Load事件
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
IncreaseGoNum();
}
}
//父页面跳转
Redirect(string.Format("Sample.aspx?ID={0}&GoNum={1}", ID, GoNum + 1));
//子页面Page_Load事件
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GoNum = GetIntFromPage("GoNum");
}
else
{
IncreaseGoNum();
}
}
//使用
<a href="#" class="return" οnclick="history.go(<%=GoNum%>)">返回</a>