Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使DropDownList项目等于具体价格_C#_Asp.net_Calculator_Set Difference - Fatal编程技术网

C# 如何使DropDownList项目等于具体价格

C# 如何使DropDownList项目等于具体价格,c#,asp.net,calculator,set-difference,C#,Asp.net,Calculator,Set Difference,我正在使用C.为比萨店制作我的项目网站。 我需要做一个页面,你可以创建自己的比萨饼。问题是,我的客户可以选择将配料放3倍。我需要做一个下拉列表,每一个都有1x、2x和3x,我有不同的价格1x=10、2x=15、3x=20。我的问题是如何使每一个1x、2x和3x等于不同的价格,因为在最后,我想在显示价格的地方制作标签 如果您有更好的建议,请留下评论(我仍在学习C#) 提前感谢您的回复 直到现在,隐藏的代码是: } static void Main() { int first, secon

我正在使用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");

}

}

看一看带有键值对的
词典

例如:

Dictionary<string, int> pizzas = new Dictionary<string, int>();
pizzas.Add("1x", 10);
pizzas.Add("2x", 15);
pizzas.Add("3x", 20);
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
}
通过将
(所选项目)传递到
比萨饼
-字典,您只需获取
字典
-
(即价格)

更好的例子是:

Dictionary<string, int> pizzas = new Dictionary<string, int>();
pizzas.Add("1x", 10);
pizzas.Add("2x", 15);
pizzas.Add("3x", 20);
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
}

感谢您的回复:)