Binding Commerce Data to a Web Form
Most Web sites must display catalog, profile, and order data to users. To display this information on Commerce Server ASP.NET-based sites you must complete three main steps: choosing the appropriate Web server control, binding the data to the Web server control, and adding the databinding expression to the Web server control template. When you are familiar with the underlying schema of the data source to which a control is bound, creating these databinding expressions is not difficult. However, because the underlying schema and the names used to access data elements in Commerce Server 2002 are often hidden or abstracted, creating these expressions on your own could be difficult.
To simplify this task for developers, Commerce Server 2002 includes the DataBinding Expression Builder. The DataBinding Expression Builder helps you generate databinding expressions by representing the various data elements in a Commerce Server data source in the DataBinding Expression Builder user interface, and allows you to copy and paste the generated databinding expression to the template of a Web server control.
This topic walks you through the steps involved in binding commerce data to a Web form.
Choosing the appropriate Web server control
Binding data to the Web server control
Adding the databinding expression to the Web form
Choosing the Appropriate Web Server Control
When adding a Web control to your .aspx file, you must determine which Web control will properly render the data onto the page. Any single-valued instance, such as a property name, can be displayed by using the Label, Text Box, or Button Web server controls. Multi-valued instances, such as catalogs, profiles, and order forms, can only be bound to certain Web server controls. The following guidelines should be used when using the DataBinding Expression Builder to bind multi-valued instance data to Web server controls in a Commerce Server project.
Web server controls to bind to | Commerce Server namespace | Guidelines |
---|---|---|
DataGrid
DataList ListBox RadioButton Repeater |
Microsoft.CommerceServer.Runtime.Catalog | Bind to methods that have a DataSet return value. You can bind to any DataSet returned by any method in the Catalog namespace. |
DataGrid
DataList ListBox RadioButton Repeater |
Microsoft.CommerceServer.Runtime.Orders | Bind to data returned by methods that implement the System.Collections.Icollection. |
DataGrid
DataList ListBox RadioButton Repeater |
Microsoft.CommerceServer.Runtime.Profiles | Bind to the Profile or PropertyGroup class. |
After you have determined which Web server control is appropriate for the data you wish to display, complete the following procedure to add it to your Web form (the .aspx file).
To add the Web server control to the Web form
- In Microsoft Visual Studio .NET, on the File menu, point to Open, and then click Project.
- In the Open Project dialog box, select the project that contains the Web form you want to modify, and click Open. The project opens.
- On the View menu, select Toolbox. The Toolbox opens.
- In the Toolbox, click the Web server control you want to add to your Web form and drag the control to the Form design surface. Drop the control on the form.
Binding Data to the Web Server Control
After adding the Web server control to the Web form, you need to add code to bind the data to the Web server control on the codebehind page (the .aspx.cs file). First you must set the data to the Web server control, and then call the DataBind method. For example:
ProductListing.DataSource = CommerceContext.Current.CatalogSystem.GetCatalog(“Adventure Works Catalog”).GetRootProducts();
ProductListing.DataBind();
The following code samples show how to bind data to a Web server control:
Code to set shipping methods on a basket on an ASP .NET Web form
Code to manipulate a basket in an ASP .NET Web form
Code to retrieve and display profile properties
Code to list catalogs on an ASP .NET Web form
Adding the DataBinding Expression to the Web Form
After adding the Web server control to the Web form and binding the data to the Web server control, you can add the databinding expression to the Web form. The databinding expression ensures that the correct portion of data bound to the Web server control is displayed. For example, if you bind a catalog to the Web server control, but only want to display the name of the catalog, you must choose the databinding expression for the CatalogName value.
To simplify the creation of databinding expressions, Commerce Server 2002 includes the DataBinding Expression Builder. The DataBinding Expression Buidler creates syntactically correct expressions based on the schema of a given data source. The expressions created by the DataBinding Expression Builder can be used in two forms:
- Simple form. Use this to create a nonformatted expression based on the data in the current container.
- Formatted form. Use this to create a formatted expression based on the data in the current container. This option is most useful when you need to include price, date, or time information in a specific format.
Additionally, you can select an advanced option, which you use to manipulate the generated databinding expression before inserting it into the Web form.
Note
- The DataBinding Expression Builder is only available in Commerce projects. For more information about creating a Commerce Project, see Creating a Commerce Project.
To create an expression using the DataBinding Expression Builder and add it to the Web form
On the Project menu, point to Commerce Server Project, and then click DataBinding Expression Builder.
In the Commerce DataBinding Expression Builder dialog box, do the following:
Use this To do this Data Source Select the source of the data you want to bind from the drop-down menu. Data Source Tree View Expand the property group nodes and select the property you want to bind. To select more than one property, press the CTRL key while selecting the additional properties.
Simple Form Select this option to get the simple form of the expression. Formatted Form Select this option to get the DataBinder.Eval(), or formatted form of the expression. Edit Expression Select this option to manipulate the Simple Form or Formatted Form of the expression. To copy the expression to the clipboard, click Copy.
To close the Commerce DataBinding Expression Builder dialog box, click Close.
On the Tools menu, click Class View.
On the Class View tab of the Solutions Explorer window, double-click the Web form to which you want to add the expression.
On the Web form code page, in HTML view, place the cursor within the template of the Web server control to which you want to apply the databinding expression.
On the Tools menu, click Paste.
The expression now appears on the Web form.
Repeat steps 1 through 8 for each expression you want to add to the Web form.
When you have finished, on the File menu, click Save to save the Web form.
You can now rebuild the project, and view the changes in the application.
Copyright © 2005 Microsoft Corporation.
All rights reserved.