xxxx 在当前上下文中不存在

本文关键字:不存在 上下文 xxxx | 更新日期: 2023-09-27 17:57:17

>我不断收到错误

"XXXX在当前背景下不存在"。

我知道有很多类似的问题,但它们对我没有帮助,因为我不太擅长C#和 asp.net。我一直在为这段代码而苦苦挣扎。基本上,我需要计算成本(这意味着在饮料类中完成,并在"关于"页面上输出。我的代码非常混乱,因为老实说,我几乎不知道我在做什么。

饮料类:

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

错误总是针对TextBox1TextBox2TextBox3calculatePrice

xxxx 在当前上下文中不存在

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