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

