Building the Navigation System


Users navigate the ASP.NET Unleashed Sample Store with the help of a tabs user control. The tabs user control displays a tab for each product category in the Categories database table. The complete code for the tabs control is contained in Listing 31.1.

Listing 31.1 Tabs.ascx
 <%@ Import Namespace="StoreComponents" %> <Script runat="Server">   Sub Page_Load     Dim intTabIndex     intTabIndex = Context.Items( "Category" )     dlstTabs.SelectedIndex = intTabIndex     dlstTabs.DataSource = CachedData.GetCategories()     dlstTabs.DataBind()   End Sub </Script> <asp:DataList   id="dlstTabs"   RepeatColumns="10"   GridLines="Both"   BorderColor="Black"   BorderWidth="1"   CellPadding="6"   ItemStyle-CssClass="TabDefault"   SelectedItemStyle-CssClass="TabSelected"   Runat="Server">   <ItemTemplate>     <%# String.Format( _        "<a href=""/aspnetstore/Category.aspx?cat={0}"">{1}</a>", _        Container.ItemIndex, _        Container.DataItem( "CategoryName" ) ) %>   </ItemTemplate> </asp:DataList> 

The C# version of this code can be found on the CD-ROM.

The bulk of the tabs control consists of a single DataList control. A DataList control is perfect for displaying a tab list because a DataList can automatically display content in a horizontal HTML table.

The tabs control is bound to its data source in the Page_Load subroutine. The records for the Categories table are retrieved through the GetCategories() method of the CachedData component. Because the GetCategories() method caches the Categories table in memory, you can use the tabs control without worrying about its impact on the performance of your Web site.

When you click a tab, you are always brought to the Category.aspx file. You should notice that a cat query string parameter is also sent. This parameter represents the currently selected tab.

The value of the cat query string variable is automatically retrieved at the start of each page request within the Global.asax file. The Global.asax file used for the store is contained in Listing 31.2.

Listing 31.2 Global.asax
 <%@ Import Namespace="StoreComponents" %> <%@ Import Namespace="System.Data" %> <Script runat="Server"> Sub Application_BeginRequest( s As Object, e As EventArgs )   Dim intCategory As Integer = -1   Dim intProductID As Integer = -1   Dim objProductInfo As DataRowView   ' Get current category index   If Request.QueryString( "cat" ) <> "" Then     intCategory = Request.QueryString( "cat" )   End If   ' Store Category in Context   Context.Items( "Category" ) = intCategory   ' Get current product ID   If Request.QueryString( "pid" ) <> "" Then     intProductID = Request.QueryString( "pid" )     objProductInfo = CachedData.GetProductRow( intProductID )     Context.Items( "ProductInfo" ) = objProductInfo     Context.Items( "ProductName" ) = objProductInfo( "ProductName" )     Context.Items( "UnitPrice" ) = objProductInfo( "UnitPrice" )   End If   ' Store ProductID in Context   Context.Items( "ProductID" ) = intProductID End Sub </Script> 

The C# version of this code can be found on the CD-ROM.

The BeginRequest event is raised right before a page request is processed . In the Global.asax file in Listing 31.2, the cat query string variable is retrieved. Next, it is added to the Items collection of the HttpContext object.

What's the purpose of the HttpContext Items collection? You should think of it as a universal portal. You can use the HttpContext Items collection to pass objects from the Global.asax to an Active Server Page or even from the Global.asax file to a user control or business component. The HttpContext Items collection is accessible wherever you go.

The current Category is added to the HttpContext Items collection in Listing 31.2 for the benefit of the Category.aspx page. The Category.aspx page retrieves the current Category to display the proper set of products when you click a tab.



ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net