-1

I am working on my project website for pizzeria using C#. I need to make page where you can create your own pizza. The problem is that my client has option to put ingredient 3x times. I need to make a drop down list with 1x, 2x and 3x for each I have different price 1x = 10 , 2x = 15, 3x = 20. My question is how I can make each 1x, 2x and 3x equal to different price because at the end I am suppose to make label where the price is shown.

If you have better suggestions, please leave a comment ( I am still learning C#) Thanks in advance for the respond.

Code behind til now is:

}

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");

}

}

dBrute
  • 1
  • 1

1 Answers1

0

Take a look at Dictionaries, with a key-value pair.

For example:

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

If someone selects "2x", you can store that value in a string in the selectedIndexChanged-event of the DropDownList. Lets call it string selected = "2x";

Now you can get the price by doing:

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

You are simply getting the Dictionary-value (which is the price) by passing the Key (selected item) to the pizzas-dictionary.

A better example:

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
}
Swag
  • 2,090
  • 9
  • 33
  • 63