Share via


Code to navigate Catalog products on an ASP.NET Web form

Use the following code to navigate through catalog products on an ASP.NET Web form. The code sample should be used within a Commerce Server project. For more information about creating a Commerce Server project, see Creating a Commerce Project.

<%@ Page Language="C#" Trace="False" %>
<%@ Import Namespace="Microsoft.CommerceServer.Runtime" %>
<%@ Import Namespace="Microsoft.CommerceServer.Runtime.Catalog" %>
<%@ Import Namespace="System.Data" %>

<script language="C#" >
    CatalogContext catalogCtx = CommerceContext.Current.CatalogSystem;
    string catalogName = @"Adventure Works Catalog";

    void Page_Load(object source, EventArgs args) {
        if (!IsPostBack) {
            UpdateCategories();
        }
    }

    void UpdateCategories() {
        RootCategoryListing.DataSource = catalogCtx.GetCatalog(catalogName).GetRootCategories();
        RootCategoryListing.DataBind();
    }

    void ProductListing_ItemCommand(object source, RepeaterCommandEventArgs args) {

        // The product id is set as part of the LinkButtons dynamically created
        // by the Repeater control.
        string productId = (string)args.CommandArgument;

        // Retrieve the details for the selected product from the Catalog
        // System.
        DataSet details =
            catalogCtx.GetCatalog(catalogName).GetProduct(productId).GetProductProperties();

        // The default row of the returned DataSet contains the product
        // details.
        DataRow product      = details.Tables[0].Rows[0];
        SelectedProduct.Text = @"You selected <b>" + (string)product["Name"] + "</b>";
        ProductID.Text       = @"Product ID: <b>" + (string)product["ProductID"] + "</b>" ;
        Desc.Text            = @"Description: <b>" + (string)product["Description"] + "</b>";
    }

    // This method is used by the nested Repeater controls used for iterating
    // through the available products in a particular category.
    DataSet GetProductListing(string category) {
        return catalogCtx.GetCatalog(catalogName).GetCategory(category).GetProducts();
    }

</script>

<html>
<head>
    <link rel="stylesheet" href="../util/style.css">
    <title>List Products</title>
</head>
<body>

    <form >

        <asp:Label
            id="SelectedProduct"
            Text="No product selected"
            /><br/>

        <asp:Label
            id="ProductID"
            /><br/>

        <asp:Label
            id="Desc"
            /><br/>

        <asp:Repeater
            id="RootCategoryListing"
            >

            <HeaderTemplate>
                <h2>Categories:</h2>
            </HeaderTemplate>

            <ItemTemplate>
                <p>
                <b><%# (string)((DataRowView)Container.DataItem)["CategoryName"] %></b><br/>

                    <asp:Repeater
                        id="ProductListing"
                        OnItemCommand="ProductListing_ItemCommand"
                        DataSource='<%# GetProductListing((string)((DataRowView)Container.DataItem)["CategoryName"]) %>'
                        >

                        <ItemTemplate>

                            <asp:LinkButton
                                id="SelectProductBtn"
                                CommandName="Select"
                                CommandArgument='<%# ((DataRowView)Container.DataItem)["ProductID"] %>'
                                >

                                <%# (string)((DataRowView)Container.DataItem)["Name"] %>

                            </asp:LinkButton>

                        </ItemTemplate>

                        <SeparatorTemplate>
                            ,&nbsp;
                        </SeparatorTemplate>

                    </asp:Repeater>
                </p>
            </ItemTemplate>

            <SeparatorTemplate>
                <p/>
            </SeparatorTemplate>

        </asp:Repeater>

    </form>

</body>
</html>

See Also

CommerceContext Class (BCL)

Microsoft.CommerceServer.Runtime.Catalog

Copyright © 2005 Microsoft Corporation.
All rights reserved.