Noah Hornak Noah Hornak - 2 months ago 6
ASP.NET (C#) Question

Adding Summary Variables

I have an asp.net website with two forms. The first form contains input controls for a user to enter shipping information. The second form contains summary information. The problem I have is that when a user adds an item by pressing the

addButton
on the first form, they should be able to enter another item and the sum of the price of those items should be passed to the summary form, instead it just passes the price of the most current item entered after
addButton
is clicked. I'm just beginning asp.net so any help would be appreciated.

protected void addButton_Click(object sender, EventArgs e)
{
var dollA = new List<decimal>();
int i = 0;
for (i = 0; i < 4; i++) {
weightInteger = int.Parse(weightTextBox.Text);
quantityInteger = int.Parse(quanTextBox.Text);
priceDecimal = decimal.Parse(priceTextBox.Text);

// Calculate the current item price.
currentPriceDecimal = priceDecimal * quantityInteger;
// Format and display the current item price.
currentTextBox.Text = currentPriceDecimal.ToString("C");

// Calculate the dollar amount due.
dollarAmountDecimal += currentPriceDecimal;

dollA.Add(dollarAmountDecimal);
dollDec = dollA.Sum();
Session["Amount"] = dollDec;
}
}


Summary Form:

protected void Page_Load(object sender, EventArgs e)
{
decimal amount;

amount = Convert.ToDecimal(Session["Amount"]);

amountTextBox.Text = amount.ToString("C");
}

Answer

This seemed to work for the OP based on the comments.

protected void addButton_Click(object sender, EventArgs e)
{
    if (Session["Amount"] == null)
        Session["Amount"] = Decimal.Zero;

    weightInteger = int.Parse(weightTextBox.Text);
    quantityInteger = int.Parse(quanTextBox.Text);
    priceDecimal = decimal.Parse(priceTextBox.Text);

    // Calculate the current item price.
    currentPriceDecimal = priceDecimal * quantityInteger;
    // Format and display the current item price.
    currentTextBox.Text = currentPriceDecimal.ToString("C");

    // Calculate the dollar amount due.
    dollarAmountDecimal += currentPriceDecimal;

    Session["Amount"] = (decimal)Session["Amount"] + dollarAmountDecimal;
}