1

好的,我制作了一个简单的程序,它有一个网络表格,您可以在其中填写详细的水果名称、公斤数和卡路里数。然后我使用会话变量从默认页面上的表单中获取水果名称,并将它们显示在下拉菜单中的关于页面上。这一切都很好,我似乎无法解决的是关于页面上的如何获取它,以便用户从下拉列表中选择一个项目(从默认页面上的表单创建)然后输入一个他们想要多少的整数(在文本中框)并在关于页面的列表框中有选择和数量输出。我将发布到目前为止的代码,任何帮助将不胜感激。

默认页面

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

关于页面

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

1 回答 1

1

您不一定需要单独的类来计算成本,但我建议您使用 aLabel来显示所选水果、所需数量和总价,如下所示About

创建一个Button包含Calculate点击事件处理程序、一个calculatePrice方法、一个TextBox用于数量和一个Label用于显示的文本,如下所示:

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;
}
于 2013-11-10T12:25:36.370 回答