Beep
Beep

Reputation: 2823

asp.net c# using session data to output more data

Ok guy, i have made a simple program that has a web form where you fill in details fruit name, kg and cal count. i have then used session variables to get the fruit name from the form on default page and display them on about page in a drop down menu. that's all working fine, what i cant seem to work out is on the about page how to get it so the user selects a item from the drop down (created from form on default page) then enter a int how many they want (in text box) and have there selection and amount output on a list box on about page. il post the code i have so far any help would be much appreciated.

default page

 public class Fruit
    {

        private string fName;
        private int grams, calsPerGram;
        private bool edible;

        public Fruit(string n, int g, int c, bool e)
        {
            grams = g;
            calsPerGram = c;
            edible = e;
            fName = n;
        }

        public int totalCalories()
        {
            return grams * calsPerGram;
        }
        public string getFruitInfo()
        {
            string s;
            if (edible == true)
            {
                s = fName + " is yummy and it has " + totalCalories() +
 "calories";
            }
            else
            {
                s = "Hands off! Not edible";
            }
            return s;
        }
    }

    public partial class _Default : System.Web.UI.Page
    {

        List<Fruit> myBasket;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                myBasket = new List<Fruit>();
                Session["listSession"] = myBasket;// seassion start
            }
        }



        protected void Button1_Click1(object sender, EventArgs e)
        {

           // Session["Fruitname"] = TbxName.Text; // my session i have made

            MyFruit = Session["Fruitname"] as List<string>;
            //Create new, if null
            if (MyFruit == null)
                MyFruit = new List<string>();

            MyFruit.Add(TbxName.Text);

            Session["Fruitname"] = MyFruit;



            abc.Items.Clear();
            Fruit f = new Fruit(TbxName.Text, int.Parse(TbxWeight.Text),
            int.Parse(TbxCal.Text), CheckBox1.Checked);

            myBasket = (List<Fruit>)Session["listSession"]; // session used
            myBasket.Add(f);
            foreach (var item in myBasket)

            {
                abc.Items.Add(item.getFruitInfo()); // List box used
            }
        }

        public List<string> MyFruit { get; set; }
    }

About page

public partial class About : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        MyFruit = Session["Fruitname"] as List<string>;
        //Create new, if null
        if (MyFruit == null)
            MyFruit = new List<string>();
        DropDownList1.DataSource = MyFruit;
        DropDownList1.DataBind();

    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Drinklabel.Text = "Your Chosen Beverage is A " + DropDownList1.SelectedValue.ToString() + " Drink.";
    }

    public List<string> MyFruit { get; set; }
}

Upvotes: 1

Views: 677

Answers (1)

Karl Anderson
Karl Anderson

Reputation: 34844

You do not necessarily need a separate class for calculating cost, but I recommend that you use a Label to display the selected fruit, amount desired and total price, like this in your About page:

Create a Button with Calculate text that has a click event handler, a calculatePrice method, a TextBox for quantity and a Label for display, like this:

protected void ButtonCalculate_Click(sender object, EventArgs e)
{
    decimal total = calculatePrice(DropDownList1.SelectedItem.Text,  
                                   TextBoxQuantity.Text.Trim());

    LabelResult.Text = "You would like " + TextBoxQuantity.Text.Trim() + 
        DropDownList1.SelectedItem.Text + "(s) for a total of $" + 
        total.ToString(); 
}

private decimal calculatePrice(string fruitName, int quantity)
{
    // Ask the database for the price of this particular piece of fruit by name
    decimal costEach = GoToDatabaseAndGetPriceOfFruitByName(fruitName);

    return costEach * quantity;
}

Upvotes: 1

Related Questions