Share via


How to Bind Property Groups and Property Collections to Web Controls

For the latest version of Commerce Server 2007 Help, see the Microsoft Web site.

You can bind property groups and property collections to a Web control, such as a list box, drop-down list, or table. This topic shows how to bind a list box named PropertyGroupBox to property groups and another list box named PropertyBox to the properties collection for a selected property group. It does this by retrieving a profile using an e-mail address entered into a text box named eMail on the Web page.

To bind a property group to a list box

  1. Create an instance of the ProfileContext runtime object.

  2. Retrieve the profile for a user by calling the GetProfile method of the ProfileContext object and passing it the e-mail address that is specified in the eMail text box.

  3. Set the DataSource property of the property group box list box to the PropertyGroups method of the ProfileContext object.

  4. Set DataTextField and DataValueField properties to "Name", which indicates that the name of the ProfilePropertyGroup will be bound to the list box.

  5. Bind the list box by calling the DataBind method.

To bind a property collection to a list box

  1. Create an instance of the ProfileContext runtime object.

  2. Retrieve the profile for a user by calling the GetProfile method of the ProfileContext object and passing it the e-mail address that is specified in the eMail text box.

  3. Set the DataSource property of the property box list box to a specific index of the ProfilePropertyGroup in the ProfilePropertyGroupCollection collection. Do this by querying the SelectedValue property of the PropertyGroupBox list box.

  4. Set the DataTextField and DataValueField properties to "Name", which indicates that the name of the ProfileProperty will be bound to the list box.

  5. Bind the list box by calling the DataBind method.

Example

The following code example shows how to bind a list box Web control to the property groups for a profile. You do this in the Page_Load event so that the control is populated when the form is displayed. The code also shows how to respond to the SelectedIndexChanged event when an item in the PropertyGroupBox list box is selected. It takes the selected property group item and populates the property collection for the property group.

using System;
using Microsoft.CommerceServer.Runtime;
using Microsoft.CommerceServer.Runtime.Profiles;

public partial class MainPage : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                // Get the Profiles runtime object.
                ProfileContext ctxt = 
                   CommerceContext.Current.ProfileSystem;

                // Get the profile from the e-mail text box.
                Profile prof = ctxt.GetProfile("email_address", 
                    eMail.Text, "UserObject");

                // Specify the property groups for the profile 
                // as the data source.
                PropertyGroupBox.DataSource = prof.PropertyGroups;

                // Specify properties to bind.
                PropertyGroupBox.DataTextField = "Name";
                PropertyGroupBox.DataValueField = "Name";

                // Bind the property groups.
                PropertyGroupBox.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write("Exception: {0}\r\nMessage: {1}", 
                    ex.GetType(), ex.ToString());
                if (ex.InnerException != null)
                {
                    Response.Write(
                        "\r\nInner Exception: {0}\r\nMessage: {1}", 
                        ex.InnerException.GetType(), 
                        ex.InnerException.Message);
                }
            }
        }
    }
    protected void PropertyGroupBox_SelectedIndexChanged(object sender, 
        EventArgs e)
    {
        // Get the Profiles runtime object.
        ProfileContext ctxt = CommerceContext.Current.ProfileSystem;

        // Get the profile from the e-mail text box.
        Profile prof = ctxt.GetProfile("email_address", eMail.Text, 
            "UserObject");

        // Specify the properties of the currently selected property 
        // group as the data source.
        PropertyBox.DataSource = 
            prof.PropertyGroups[PropertyGroupBox.SelectedValue.ToString()].Properties;

        // Specify properties to bind.
        PropertyBox.DataTextField = "Name";
        PropertyBox.DataValueField = "Name";

        // Bind the properties.
        PropertyBox.DataBind();
    }
}

See Also

Other Resources

Profiles Concepts and Tasks

Developing with the Profiles System