惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
IT之家
IT之家
D
Docker
博客园 - 叶小钗
A
About on SuperTechFans
博客园_首页
Apple Machine Learning Research
Apple Machine Learning Research
Recorded Future
Recorded Future
Stack Overflow Blog
Stack Overflow Blog
腾讯CDC
V
V2EX
S
SegmentFault 最新的问题
量子位
P
Proofpoint News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
Latest news
Latest news
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
有赞技术团队
有赞技术团队
The GitHub Blog
The GitHub Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
I
InfoQ
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
D
DataBreaches.Net
G
GRAHAM CLULEY
P
Proofpoint News Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Microsoft Security Blog
Microsoft Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Y
Y Combinator Blog
小众软件
小众软件
NISL@THU
NISL@THU
L
Lohrmann on Cybersecurity
aimingoo的专栏
aimingoo的专栏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
I
Intezer
Last Week in AI
Last Week in AI
T
Threatpost
人人都是产品经理
人人都是产品经理
U
Unit 42
Security Latest
Security Latest
AWS News Blog
AWS News Blog
T
The Blog of Author Tim Ferriss
MongoDB | Blog
MongoDB | Blog
罗磊的独立博客
GbyAI
GbyAI
P
Palo Alto Networks Blog
G
Google Developers Blog
MyScale Blog
MyScale Blog
L
LangChain Blog

博客园 - dyq

ASP.NET架构分析 ASP.NET 页面对象模型 基于aspnet Forms身份验证基本原理 - dyq - 博客园 实现简单的 Forms 身份验证 利用HttpModuler实现WEB程序同一时间只让一个用户实例登陆 建立一个使用.Net 2.0 MemberShip功能的标准例程(二)——配置篇 GridView 72般绝技 SQL Server 连接字符串和身份验证 .NET程序中常用的代码 - dyq - 博客园 分析ASP.NET服务器控件开发-控件生命周期 收藏网站制作常用经典ajax.prototype.javascript实例打包下载 asp.net中<%%>形式的用法(原创) .NET模板引擎 EFPlatform.CodeGenerator (代码生成器 静态页生成 生成HTML 模板) 深入浅出之正则表达式(一) 正则表达式学习 常用的正则表达式 ASP.NET URL Rewrite. URL重写 ASP.NET 生成静态页 .NET生成静态页面并分页
ASP.NET 中处理页面“回退”的方法
dyq · 2008-02-16 · via 博客园 - dyq

转自:

http://blog.joycode.com/moslem/archive/2006/10/17/85307.aspx

我们在编写基于 ASP.NET 的应用程序时,如果代码执行出错或检测到异常,一般会提示用户“返回”或“回退”,或者在多步操作、列表/详细的查看界面中,也会给用户提供回退到上一页 面的链接,对于这种情况,大家很快就会想到的简单做法就是利用 Javascript 来实现,即 history.go(-1) ,但是由于 ASP.NET 页面的 PostBack 机制,所以 history.go(-1) 可能还是当前页面,而不能真正回退到上一页面。

在 Classifieds Site Starter Kit 中,学习到一种不错的关于回退的处理方法,可以分别在客户端和服务器控件中实现页面的回退,代码如下:

1)首先在页面中增加两个属性

//记录上一个页面的信息

private string UrlReferrer { get { return ViewState["UrlReferrer"] as string; } set { ViewState["UrlReferrer"] = value; } } //记录 PostBack 的次数 public int NumPostBacks { get { if (ViewState["NumPostBacks"] != null) return (int)ViewState["NumPostBacks"]; else { ViewState["NumPostBacks"] = 0; return 0; } } set { ViewState["NumPostBacks"] = value; } }  

2)在 Page_Load 事件记录上一页面地址、更新 Postback 次数、设置回退链接的地址

// 记录上一页面的信息或更新 PostBack 的次数 protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { if (Request.UrlReferrer != null) this.UrlReferrer = Request.UrlReferrer.ToString(); } else NumPostBacks++; int goBackSteps = NumPostBacks + 1; BackLink.NavigateUrl = String.Format("javascript:history.go(-{0});", goBackSteps); }

3)直接在代码中处理回退操作(如 Back_Click),可以直接调用如下方法

//在代码中回退 protected void ReturnToPreviousPage() { string referrer = UrlReferrer; if (referrer != null) Response.Redirect(referrer); else Response.Redirect("~/default.aspx", true); }