Common ASP.NET Code Techniques (DPC&DWC Reference)--6
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
'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
| 对此文章发表了评论 |

