0

I am programming in visual studio. I have a dropdown list where user is able to choose among different products, and price to the corresponding products appears in a label in front of it.

I have a button name add to cart, and a dropdown list under it.

What I want is: as soon as a product is chosen in the first drop down list, after clicking add to cart, that product should also come in the second dropdown list (in my case: Items in cart). As many as products are chosen, it should come to the drop down list; pretty much like a real life Add to cart option.

And also a calculated price for all the products stored in the cart will be displayed in a label that I have created.

[Please ask me if you need any clarification about my question].

Store.aspx:

    <form id="form1" runat="server">
    <div>
       Select Products: <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="GetPrice"  AutoPostBack="True"> 

                 </asp:DropDownList>
 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [productdata]"></asp:SqlDataSource>
 &nbsp;Price:
        <asp:Label ID="Label1" runat="server" ></asp:Label>
        <br />
         <asp:Button ID="Button1" runat="server" Text="Add to Cart" OnClick="Button1_Click" />
        <br />
        Items in cart: <asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>

        <br />

 Total Price: <asp:Label ID="Label2" runat="server"></asp:Label>
 </div>
    </form> 

Store.aspx.cs:

        protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Label3.Text = Request.QueryString["name"];//show welcome text
            String cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            using (SqlConnection sc = new SqlConnection(cs))
            {
                SqlCommand sqlcom = new SqlCommand("Select id, productname, price from productdata", sc);
                sc.Open();
                //DropDownList1.Items.Add(new ListItem("0", "select one"));
                DropDownList1.DataTextField = "productname";//show in the dropdown list
                DropDownList1.DataValueField = "price"; //show in the label
                DropDownList1.DataSource = sqlcom.ExecuteReader();

                //DropDownList1.Items.Insert("0", new ListItem("choose value"));

   DropDownList1.DataBind();
                DropDownList1.Items.Insert(0, new ListItem("select one", "0"));
            }
        }
    }

    protected void GetPrice(object sender, EventArgs e)
    {
        Label1.Text = DropDownList1.SelectedValue;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
       //add to the cart
    }

1 Answers1

0

On Button1_Click you should do two easy things:

  1. You need to take the selected item from the first DropDownList and add it to the Cart's DropDownList, using DropDownList2.Items.Add(selectedItem);

  2. To show the total price you need to sum up all the DropDownList items using:

    DropDownList2.Items.Cast().Select(a => Convert.ToInt32(a.Value)).Sum();

    You can definitely read more about summing up DropDownList items in a previous SO post:

    How to sum all values of a dropdown list

In Conclusion

This should work for you:

protected void Button1_Click(object sender, EventArgs e)
{
    // Get the selected item
    ListItem item = DropDownList1.SelectedItem;

    // Reset selected to false
    item.Selected = false;

    // Add to the cart dropdown
    DropDownList2.DataTextField = "productname";
    DropDownList2.DataValueField = "price";
    DropDownList2.Items.Add(item);
    DropDownList2.DataBind();

    int sum = DropDownList2.Items.Cast<ListItem>().Select(a => Convert.ToInt32(a.Value)).Sum();
    Label2.Text = Convert.ToString(sum);
}

Hope it helps! If you have any more questions I will be happy to help

Community
  • 1
  • 1
Jaqen H'ghar
  • 16,186
  • 8
  • 49
  • 52
  • Thanks very much, it almost works though. only problem is it can't add multiple items selected into cart. Can u pleas help me to solve it? I will accept your answer shortly. The error: Cannot have multiple items selected in a DropDownList. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. – Zen Axelsson May 14 '16 at 22:01
  • @ZenAxelsson You cannot have multiple items selected in a `DropDownList`. You can change it if you want to a `ListBox`. Change `` to this line: ``. You can read more about it in [this link](http://stackoverflow.com/questions/18824638/select-multiple-value-in-dropdownlist-using-asp-net-and-c-sharp) – Jaqen H'ghar May 14 '16 at 22:16
  • Idan b , yep, u r very right. thing is working according to my expectation now. great Idan. – Zen Axelsson May 14 '16 at 22:22