您现在的位置: 无忧电子商务网 >> 信息学院 >> 程序开发 >> asp.net >> 正文

Common ASP.NET Code Techniques (DPC&DWC Reference)--6

作者:作者:未…    信息学院来源:网络收集    点击数:    更新时间:2006-8-28 我要参与讨论

  uot;]).Clear();
12:   }
13:  }
14:
15: </script>
16:
17: <html>
18: <body>
19:   Your navigation history has been cleared!
20: </body>
21: </html>
Listing 2.1.6 contains the code for ClearStackHistory.CSharp.aspx. This code only has a single task—clear the contents of the navigation history stack—and therefore is fairly straightforward. The ASP.NET page starts by checking to determine if Session["History"] refers to a Stack object instance (line 6). If it does, the Clear method is used to erase all the stack's elements (line 11).

The code for the second utility page, Back.CSharp.aspx, can be seen in Listing 2.1.7.

Listing 2.1.7 Back.CSharp.aspx Sends the User to the Previous Page in His Navigation History Stack
1: <script language="c#" runat="server">
2:  void Page_Load(Object sender, EventArgs e)
3:  {
4:   // See if we have a stack created or not:
5:   if (Session["History"] == null ||
6:     ((Stack) Session["History"]).Count < 2)
7:   {
8:    // There's no Stack, so we can't go back!
9:    Response.Write("Egad, I can't go back!");
10:   } else {
11:    // we need to go back to the prev. page
12:    ((Stack) Session["History"]).Pop();
13:    Response.Redirect(((Stack) Session["History"]).Pop().ToString());
14:   }
15:  }
16: </script>
As with ClearStackHistory.CSharp.aspx, Back.CSharp.aspx starts by checking to determine if Session["History"] is null. If that is the case, a warning message is displayed because we can't possibly step back through our navigation history stack if it doesn't exist!

Take a moment to briefly look over Listing 2.1.5 again. Note that on each page we visit, we add the current URL to the stack. Therefore, if we want to go back to the previous page, we can't just pluck off the top element from the stack (because that contains the current URL). Rather, we must pluck off the top-most item, dispose of it, and then visit the next item on the top of the stack. For that reason, our stack must have at least two elements to be able to traverse back to the previous page. On line 6, we check to make sure that the navigation history stack contains at least two elements.

Given that we have a properly defined navigation history stack—that is, Session["History"] is not null and there are at least two elements in the Stack—we will reach lines 12 and 13, which do the actual work of sending the user back to the previous page. Line 12 simply disposes of the top-most Stack e

上一页  [1] [2] [3] [4] [5] [6] 下一页

在google里搜索更多Common ASP.NET Code Techniques (DPC&DWC Reference)--6

Google
Web www.51ec.org
【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
我来说两句 对此文章发表了评论
  昵 称: *必填    ·注册用户·
  评 分: 1分 2分 3分 4分 5分     严禁发表危害国家安全、政治、黄色淫秽等内容的评论,用户需对自己在使用本网站服务过程中的行为承担法律责任。本站管理员有权保留或删除评论内容,评论内容只代表机友个人观点,与本网站立场无关。  
评 论
内 容

 
评论列表 (最新 评论仅限网友观点!)

供求信息




| 设为首页 | 加入收藏 | 关于我们 | 广告服务 | 联系方式 | 友情链接 | 版权申明