简体   繁体   中英

How can I get my variables to carry from dropdownlist asp.net to total and print out in c#?

I am trying to make a shopping cart that will add the selected items from the dropdownlist with a quantity and add to cart. The user should be able to do this as many times as they want before clicking the Checkout button. By clicking the Checkout button, it will print out the quantities totaled of each item and the total of the entire purchase.

The outputs are all coming out as zero in the print line. Zero quantity and zero totalPrice. I am trying to figure out how to get the variables to carry into the print line.

Here is the ASP.net side

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MidtermExamPart1.aspx.cs" Inherits="MidtermExam.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h3>Shopping Cart</h3><br /><br />
        <asp:Dropdownlist ID="droplist" runat="server"></asp:Dropdownlist><br />
        Quantity: <asp:TextBox ID="qty" runat="server" Width="25px"></asp:TextBox>
        <asp:Button ID="AddToCart" runat="server" OnClick="AddToCart_Click" Text="Add to Shopping Cart" style="margin-left: 43px" Width="140px" /><br /><br /><br />

        <asp:Button ID="Checkout" runat="server" Text="Checkout" OnClick="Checkout_Click" /><br /><br />

        <asp:Label ID="message" runat="server" Text=""></asp:Label>
    </div>

    </form>
</body>
</html>

and here is the c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MidtermExam
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                droplist.Items.Add(new ListItem("LEAN IN ($19.99)", "19.99"));
                droplist.Items.Add(new ListItem("HAPPY, HAPPY, HAPPY ($15.99)", "15.99"));
                droplist.Items.Add(new ListItem("ELEVEN RINGS ($17.99)", "17.99"));
                droplist.Items.Add(new ListItem("LET'S EXPLORE DIABETES WITH OWLS ($12.99)", "12.99"));
                droplist.Items.Add(new ListItem("THE GUNS AT LAST LIGHT ($16.99)", "16.99"));
            }
         }

        private double quantityLean;
        private double quantityHappy;
        private double quantityRings;
        private double quantityOwls;
        private double quantityGuns;
        private double totalPrice;

        protected void AddToCart_Click(object sender, EventArgs e)
        {
            string serviceString = droplist.SelectedItem.Text;

            double quantity = Convert.ToDouble(Request["qty"]);

            if (serviceString == "LEAN IN ($19.99)")
            {
                this.quantityLean = this.quantityLean + quantity;
                this.totalPrice = this.totalPrice + (quantity * 19.99);
            }
            if (serviceString == "HAPPY, HAPPY, HAPPY ($15.99)")
            {
                this.quantityHappy = this.quantityHappy + quantity;
                this.totalPrice = this.totalPrice + (quantity * 19.99);
            }
            if (serviceString == "ELEVEN RINGS ($17.99)")
            {
                this.quantityRings = this.quantityRings + quantity;
                this.totalPrice = this.totalPrice + (quantity * 19.99);
            }
            if (serviceString == "LET'S EXPLORE DIABETES WITH OWLS ($12.99)")
            {
                this.quantityOwls = this.quantityOwls + quantity;
                this.totalPrice = this.totalPrice + (quantity * 19.99);
            }
            if (serviceString == "THE GUNS AT LAST LIGHT ($16.99)")
            {
                this.quantityGuns = this.quantityGuns + quantity;
                this.totalPrice = this.totalPrice + (quantity * 19.99);
            }
        }

        protected void Checkout_Click(object sender, EventArgs e)
        {
            string newMessage = String.Format("You orderered:\n\nLEAN IN ($19.99) QTY:{0}\nHAPPY, HAPPY, HAPPY ($15.99) QTY: {1}\nELEVEN RINGS ($17.99) QTY: {2}\nLET'S EXPLORE DIABETES WITH OWLS ($12.99) QTY: {3}\nTHE GUNS AT LAST LIGHT ($16.99) QTY: {4}\n\nYour total cost then would be {5:C2}", this.quantityLean, this.quantityHappy, this.quantityRings, this.quantityOwls, this.quantityGuns, this.totalPrice);
            message.Text = newMessage;
        }       
    }
}

I have been working on this and searching forums for hours! Please help guide me if you can

Just Modify your

string serviceString = droplist.SelectedItem.Text;
double quantity = Convert.ToDouble(qty.Text);
var Value=Convert.ToDecimal(droplist.SelectedItem.value);

Now use a grid to populate the summary

DataTable dt=new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Price");
dt.Columns.Add("Quantity");
dt.Columns.Add("Total");
dt.Rows.Add(new [] {serviceString ,Value,quantity ,Value*quantity });

set this Table to your grids data source and for future use store it in Session (Not a Good Idea but for quick solution its Ok)

if(Session["List"]!=null)
dt=(DataTable)Session["List"];

This is basic but if you need really a good solution then I can provide something

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM