-1

我正在使用C#在我的比萨店项目网站上工作。我需要制作页面,您可以在其中创建自己的披萨。问题是我的客户可以选择将配料放 3 倍。我需要为每个1x、2x 和 3x制作一个下拉列表,我有不同的价格 1x = 10、2x = 15、3x = 20。我的问题是如何使每个 1x、2x 和 3x 等于不同的价格,因为最后,我想在显示价格的地方制作标签。

如果您有更好的建议,请发表评论(我还在学习 C#)提前感谢您的回复。

到现在为止的代码是:

}

static void Main()
{
    int first, second, third;
    first = 10;
    second = 15;
    third = 20;
}


protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    if (CheckBox1.Checked == true) 
    {
        DropDownList1.Visible = true;
        Image1.Visible = true;
    }
    else
    {
        DropDownList1.Visible = false;
        Image1.Visible = false;
    }
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{

   // Each element = to different price 
      DropDownList1.DataValueField = "first";
    //ListItem lst = new ListItem("Add New", "0");

}

}

4

1 回答 1

0

看看Dictionaries, 带有一个键值对。

例如:

Dictionary<string, int> pizzas = new Dictionary<string, int>();
pizzas.Add("1x", 10);
pizzas.Add("2x", 15);
pizzas.Add("3x", 20);

如果有人选择“2x”,您可以将该值存储selectedIndexChangedDropDownList. 让我们称之为string selected = "2x";

现在您可以通过以下方式获取价格:

int price = pizzas[selected]; // this will return 15 (key '2x' is bound to value '15', see dictionary)

您只是通过将(选定项目)传递给-dictionary来获得Dictionary- value(即价格) 。Keypizzas

一个更好的例子:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddl = (DropDownList)sender;
    string selected = ddl.SelectedValue.ToString(); // lets select "2x" 
    int price = pizzas[selected]; // this will return 15
    //Here you can set the Price Value in the Label
}
于 2014-06-15T17:04:29.483 回答