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>
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
}