I'm going to repost this series of "development tips" related to Commerce Server 2007 to help other folks get started with their development work. All of these "tips" come directly from what I've learned (the hard way) over the past eight months and are intended to save you time and effort. All code samples are now based upon the RTM version and have been tested in our production environment.
The Commerce Server Team has spent a lot of time and effort making the site developer's life a whole lot easier. Sometimes it's through really big changes like the new Inventory subsystem and sometimes it's through the "little things" like the RegionCodeDataSet.
Tip #5 - Using the RegionCodeDataSet
One of the really interesting little things I stumbled upon during my CS2007 explorations was the RegionCodeDataSet which contains two DataTables, one for Country Codes and another for State or Province Codes.
On my site I've setup a simple form for a user to add a new Address profile as shown below. Since our site is used globally, I need to make sure the Address profile is filled out as completely and correctly as possible. As you can see, I begin with a DropDownList of countries which is bound to the RegionCodeDataSet.CountryCodes DataTable.
I've also bound another DropDownList control to the RegionCodeDataSet.StateCodes DataTable using the Page_Load code shown below.
protected void Page_Load(object sender, EventArgs e)
{ try
{ if (!Page.IsPostBack)
{ // Get the RegionCodeDataSet
dsRegions = addressHelper.GetRegionCodes();
// Get the CountryCodesDataTable
dtCountryCodes = dsRegions.CountryCodes;
CountryCodeDropDownList.DataSource = dtCountryCodes;
CountryCodeDropDownList.DataBind();
// Allow AutoPostBacks
CountryCodeDropDownList.AutoPostBack = true;
// Set the DropDownList's initial value
CountryCodeDropDownList.SelectedValue = "US";
// Get the DropDownList's SelectedIndex
int iRow = CountryCodeDropDownList.SelectedIndex;
// Get the DataRow array
DataRow[] drStates = dtCountryCodes.Rows[iRow].GetChildRows("States");
// If the "Country" has "States"
if (drStates.Length > 0)
{ RegionCodeDropDownList.DataSource = drStates;
RegionCodeDropDownList.DataBind();
RegionCodeDropDownList.Enabled = true;
RegionCodeLabel.Enabled = true;
}
CountryCodeDropDownList.Focus();
}
}
catch (Exception ex)
{ // Show the Error Label and set it's text
ErrorLabel.Visible = true;
ErrorLabel.Text = ex.Message;
// Hide the Panel control
AddressPanel.Visible = false;
}
}
The Page_Load method calls a simple AddressHelper.GetRegionCodes() method shown here.
public RegionCodeDataSet GetRegionCodes()
{ // New RegionCodeDataSet
RegionCodeDataSet dsRegions = new RegionCodeDataSet();
// Get the TypedDataSet from the OrderContext
dsRegions = CommerceContext.Current.OrderSystem.GetRegionCodes();
// Check to see if relation exists
if (dsRegions.Relations.Count < 1)
{ // Add a relation to each table in the Typed DataSet
dsRegions.Relations.Add("States", dsRegions.CountryCodes.Columns["Code"], dsRegions.StateCodes.Columns["CountryCode"]); }
return dsRegions;
}
When the user selects another country such as the "United Kingdom" which doesn't have any States or Provinces, the DropDownList isn't data bound and is disabled as shown below.
I then use the DropDownList control's OnSelectedIndexChanged event to call the method shown below which returns the "States" or "Provinces" depending upon the CountryCode selected by the user.
protected void GetStateCodes(object sender, EventArgs e)
{ try
{ dsRegions = addressHelper.GetRegionCodes();
dtCountryCodes = dsRegions.CountryCodes;
// Get the DropDownList's SelectedIndex
int iRow = CountryCodeDropDownList.SelectedIndex;
// Get the DataRow array
DataRow[] drStates = dtCountryCodes.Rows[iRow].GetChildRows("States");
// If the "Country" has "States"
if (drStates.Length > 0)
{ RegionCodeDropDownList.DataSource = drStates;
RegionCodeDropDownList.DataBind();
RegionCodeDropDownList.Enabled = true;
RegionCodeLabel.Enabled = true;
}
else
{ // Clear the DropDownList's Items
RegionCodeDropDownList.Items.Clear();
RegionCodeDropDownList.Enabled = false;
RegionCodeLabel.Enabled = false;
}
}
catch (Exception ex)
{ // Show the Error Label and set it's text
ErrorLabel.Visible = true;
ErrorLabel.Text = ex.Message;
}
}
The final step to wrap this code with a new Atlas UpdatePanel control so only the TextBox and DropDownList controls are re-rendered during the post-back.
When this is put all together, it creates a really nice experience for the user and ensures that the Address profile created will be complete and correct (at least as far as the country and states/provinces are concerned).
Special thanks to Joe Wasson [MSFT] for his help and guidance in pointing me in the right direction!
Technorati Tags: Commerce Server