繁体   English   中英

如何将我的变量从dropdownlist asp.net转移到total并在c#中打印出来?

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

我正在尝试制作一个购物车,它将从下拉列表中添加所选项目并添加数量并添加到购物车。 在单击“结帐”按钮之前,用户应该可以多次执行此操作。 通过单击“结帐”按钮,它将打印出每个项目的总计数量和整个购买的总数。

输出在打印行中都显示为零。 零数量和零总价格。 我试图弄清楚如何让变量进入打印行。

这是ASP.net方面

<%@ 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>

这是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;
        }       
    }
}

我一直在研究和搜索论坛几个小时! 如果可以,请帮助指导我

只需修改你的

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

现在使用网格填充摘要

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

将此表设置为您的网格数据源,以备将来使用将其存储在会话中(不是一个好主意,但为了快速解决它的确定)

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

这是基本的,但如果你需要一个很好的解决方案,那么我可以提供一些东西

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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