Hi, I've got a DropDownList with a list of colours, when one is selected and the "Add to ShoppingCart" button is pressed, all the colours in the DropDownList are duplicated.
Can somebody please help.
// fill the control with data
private void PopulateControls(ProductDetails pd)
{
// display product recommendations
string productId = pd.ProductId.ToString();
recommendations.LoadProductRecommendations(productId);
// display the product details
titleLabel.Text = pd.Name;
descriptionLabel.Text = pd.Description;
priceLabel.Text = String.Format("{0:c}", pd.Price);
productImage.ImageUrl = "ProductImages/" + pd.Image2FileName;
// set the title of the page
this.Title = BalloonShopConfiguration.SiteName + pd.Name;
// obtain the attributes of the product
DataTable attrTable = CatalogAccess.GetProductAttributes(productId);
// temp variables
string prevAttributeName = "";
string attributeName, attributeValue, attributeValueId;
// current DropDown for attribute values
Label attributeNameLabel;
DropDownList attributeValuesDropDown = new DropDownList();
// read the list of attributes
foreach (DataRow r in attrTable.Rows)
{
// get attribute data
attributeName = r["AttributeName"].ToString();
attributeValue = r["AttributeValue"].ToString();
attributeValueId = r["AttributeValueID"].ToString();
// if starting a new attribute (e.g. Color, Size)
if (attributeName != prevAttributeName)
{
prevAttributeName = attributeName;
attributeNameLabel = new Label();
attributeNameLabel.Text = attributeName + ": ";
attributeValuesDropDown = new DropDownList();
attrPlaceHolder.Controls.Add(attributeNameLabel);
attrPlaceHolder.Controls.Add(attributeValuesDropDown);
}
// add a new attribute value to the DropDownList
attributeValuesDropDown.Items.Add(new ListItem(attributeValue, attributeValueId));
}
}
protected void AddToCartButton_Click(object sender, EventArgs e)
{
// Retrieve ProductID from the query string
string productId = Request.QueryString["ProductID"];
// Retrieve the selected product options
string options = "";
foreach (Control cnt in attrPlaceHolder.Controls)
{
if (cnt is Label)
{
Label attrLabel = (Label)cnt;
options += attrLabel.Text;
}
if (cnt is DropDownList)
{
DropDownList attrDropDown = (DropDownList)cnt;
options = attrDropDown.Items[attrDropDown.SelectedIndex].Value;
}
}
// Add the product to the shopping cart
ShoppingCartAccess.AddItem(productId, options);
}