将DropDownList添加到购物车时重复 [英] DropDownList duplicates when added to Cart

查看:78
本文介绍了将DropDownList添加到购物车时重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我特地拿到一个DropDownList与颜色列表,当一个人selceted和加入购物车"按钮,在DropDownList重复的颜色.

谁能帮我吗?

// 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);

    }

解决方案

好像再次填充了下拉控件.

您需要在页面重新加载时绕过它才能执行AddToCartButton_Click处理程序. br/>

因为您已经放了这个...

attributeValuesDropDown.Items.Add(new ListItem(attributeValue, attributeValueId));



...每当必须在列表中添加新项目时,遍历每个项目的foreach循环中的语句就会存在于购物车中.

因此,以前的所有项目都将被再次填写,而新的仅一次.

我建议在填充声明之前使用此词句.

attributeValuesDropDown.Items.Clear()



...但是,这是不必要的清除下拉列表中存在的所有项目,然后再次添加所有相同项目.但这将消除您的问题.

否则,您需要应用一些逻辑,通过该逻辑不必将此语句放入循环...

祝你好运!


Hi, I''ve got a DropDownList with a list of colours, when one is selceted and the "Add to Cart" button is pressed, the colours in the DropDownList duplicate.

Can anyone please help me?

// 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);

    }

解决方案

Looks like you are populating the dropdown control again.

You need to by-pass it when the page reloads in order to execute AddToCartButton_Click handler.


Thanks for the reply, could you please show me how to by pass it?


Bease you''ve put this...

attributeValuesDropDown.Items.Add(new ListItem(attributeValue, attributeValueId));



...statement in a foreach loop which runs through each item exists in cart everytime a new item has to be added in list.

So all previous items will get filled in again and the new only once.

I suggest to use this stement before the statement of filling...

attributeValuesDropDown.Items.Clear()



... however this is unnecessary clearing of all items exists in dropdown list before adding all the same once again. But it will eliminate your issue.

Otherwise you need to apply some logic through which you don''t have to put this statement in loop...

Good Luck!


这篇关于将DropDownList添加到购物车时重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆