Part 3: Layout and Category Menu
by Joe Stagner
Tailspin Spyworks demonstrates how extraordinarily simple it is to create powerful, scalable applications for the .NET platform. It shows off how to use the great new features in ASP.NET 4 to build an online store, including shopping, checkout, and administration.
This tutorial series details all of the steps taken to build the Tailspin Spyworks sample application. Part 3 covers adding layout and a category menu.
Adding Some Layout and a Category Menu
In our site master page we’ll add a div for the left side column that will contain our product category menu.
[!code-aspxMain]
1: <div id="content">
2: <div id="rightColumn"></div>
3: <div id="mainContent">
4: <div id="centerColumn">
5: <asp:ContentPlaceHolder ID="MainContent" runat="server"></asp:ContentPlaceHolder>
6: </div>
7: </div>
8: <div id="leftColumn">
9: <!-- Our menu will go here -->
10: </div>
11: <div class="clear"></div>
12: </div>
Note that the desired aligning and other formatting will be provided by the CSS class that we added to our Style.css file.
[!code-cssMain]
1: #leftColumn
2: {
3: position: relative;float: left;width: 14em;padding: 2em 1.5em 2em;background: #fff url('images/a1.gif') repeat-y right top;
4: top: 1px;
5: left: 0px;
6: height: 100%;
7: }
The product category menu will be dynamically created at runtime by querying the Commerce database for existing product categories and creating the menu items and corresponding links.
To accomplish this we will use two of ASP.NET’s powerful data controls. The “Entity Data Source” control and the “ListView” control.
Let’s switch to “Design View” and use the helpers to configure our controls.
Let’s set the EntityDataSource ID property to EDS_Category_Menu and click on “Configure Data Source”.
Select the CommerceEntities Connection that was created for us when we created the Entity Data Source Model for our Commerce Database and click “Next”.
Select the “Categories” Entity set name and leave the rest of the options as default. Click “Finish”.
Now let’s set the ID property of the ListView control instance that we placed on our page to ListView_ProductsMenu and activate its helper.
Though we could use control options to format the data item display and formatting, our menu creation will only require simple markup so we will enter the code in the source view.
[!code-aspxMain]
1: <asp:ListView ID="ListView_ProductsMenu" runat="server" DataKeyNames="CategoryID" DataSourceID="EDS_Category_Menu">
2: <EmptyDataTemplate>No Menu Items.</EmptyDataTemplate>
3: <ItemSeparatorTemplate></ItemSeparatorTemplate>
4: <ItemTemplate>
5: <li>
6: <a href='<%# VirtualPathUtility.ToAbsolute("~/ProductsList.aspx?CategoryId=" +
7: Eval("CategoryID")) %>'><%# Eval("CategoryName") %></a>
8: </li>
9: </ItemTemplate>
10: <LayoutTemplate>
11: <ul ID="itemPlaceholderContainer" runat="server">
12: <li runat="server" id="itemPlaceholder" />
13: </ul>
14: <div>
15: </div>
16: </LayoutTemplate>
Please note the “Eval” statement : <%# Eval(“CategoryName”) %>
The ASP.NET syntax <%# %> is a shorthand convention that instructs the runtime to execute whatever is contained within and output the results “in Line”.
The statement Eval(“CategoryName”) instructs that, for the current entry in the bound collection of data items, fetch the value of the Entity Model item names “CatagoryName”. This is concise syntax for a very powerful feature.
Lets run the application now.
Note that our product category menu is now displayed and when we hover over one of the category menu items we can see the menu item link points to a page we have yet to implement named ProductsList.aspx and that we have built a dynamic query string argument that contains the category id.
|