xxxx在此当前上下文中不存在

时间:2015-06-21 16:58:11

标签: c# asp.net

我一直收到错误

  

" XXXX在当前上下文中不存在"。

我知道有很多类似的问题,但他们不帮助我,因为我不太擅长C#& asp.net。 我一直在努力解决这段代码问题。基本上我需要有一个计算成本(这意味着要在Beverage类中完成,并在About页面上输出。 我的代码非常混乱,因为老实说我不知道​​自己在做什么。

饮料类:

public class Beverage
    {
        string fruit;
        int kg;
        int cost;
        int cal;

        public string getOutputString()
        {
            return " Your selected Beverage is made of " + kg + " of " + fruit + ". " +
            "The cost is £ " + cost + " and has " + cal + ".";
        }

        public static int CalculatePrice()
        {
            int cost = (TextBox1.text + TextBox2.text);
            int cal = TextBox1.Text + TextBox3.Text;
        }

    }

关于代码背后:

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)
        {

        }

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

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

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

    }

错误总是针对TextBox1TextBox2TextBox3&amp; calculatePrice

1 个答案:

答案 0 :(得分:1)

您收到错误是因为您尝试访问TextBox1.Text课程以外的About。只有此类才能访问此属性,因为它继承了System.Web.UI.PageTextBox1.Text类的Beverage范围不大。如果要在该类中使用这些值,请将它们作为输入参数传递给方法。