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

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

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

  istory = ((Stack) Session["History"]).GetEnumerator();

'VB code, however, does not require an explicit cast
Dim enumHistory As IEnumerator = Session("History").GetEnumerator()
With VB, however, such a cast is not necessary. Casting issues with the Session object are discussed in more detail in Chapter 14.

After either creating a new session-level Stack instance or displaying the Stack's contents, we're ready to add the current URL to the navigation history stack. This could be accomplished with the following simple line of code:

((Stack) Session["History"]).Push(Request.Url.PathAndQuery);
However, if the user refreshed the current page, it would, again, get added to the navigation history stack. It would be nice not to have the same page repeatedly appear in the navigation history stack. Therefore, on line 23, we use the Peek method to see if the top-most element in the Stack is not equal to the current URL. If the top-most element of the stack is not equal to the current URL, we Push the current URL onto the top of the stack, otherwise we do nothing.

Before we use the Peek method, we first determine whether the Stack is empty. Recall from the previous section, "Working with the Queue Class," using the Peek method on an empty Queue will raise an InvalidOperationException exception. This is the same case with the Stack class; therefore, on line 21, we first check to ensure that at least one element is in the Stack before using the Peek method.

Two useful utility ASP.NET pages have been created to provide some extra functionality for our navigation history stack. The fist page, ClearStackHistory.Csharp.aspx, erases the contents of the history stack and is presented in Listing 2.1.6. The second page, Back.Csharp.aspx, serves like a back button in the user's browser, taking him to the previously visited page. The code for Back.Csharp.aspx is given in Listing 2.1.7. We'll examine these two code listings momentarily.

Listing 2.1.5 also contains a link to another ASP.NET page, Listing2.1.5.b.aspx. This page is identical to Listing2.1.5.aspx. In your Web site, you would need to, at a minimum, include the code in Listing 2.1.5 in each ASP.NET page to correctly keep the navigation history up-to-date.

Listing 2.1.6 ClearStackHistory.CSharp.aspx Erases the Contents of the Navigation History Stack
1: <script language="c#" runat="server">
2:
3:  void Page_Load(Object sender, EventArgs e)
4:  {
5:   // See if we have a stack created or not:
6:   if (Session["History"] == null)
7:   {
8:    // There's no Stack, so we don't need to do anything!
9:   } else {
10:    // we need to clear the stack
11:    ((Stack) Session["History&q

上一页  [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分     严禁发表危害国家安全、政治、黄色淫秽等内容的评论,用户需对自己在使用本网站服务过程中的行为承担法律责任。本站管理员有权保留或删除评论内容,评论内容只代表机友个人观点,与本网站立场无关。  
评 论
内 容

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

供求信息




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