protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request.QueryString["id"] != null) { FoodTableBind(); Food f = SQLProcedures.GetFoodForID(int.Parse(Request.QueryString["id"])); txtNazivNamirnice.Text = f.Name; foodName = f.Name; rblOdaberiTipNamirnice.SelectedValue = SQLProcedures.GetTypeForFood(int.Parse(Request.QueryString["id"])); unitsWithKcal = SQLProcedures.GetMeasuringUnitsForFoodID(int.Parse(Request.QueryString["id"])); oldUnits = new Dictionary <string, int>(unitsWithKcal); unitsWithKcalList = new List <string>(); foreach (var unit in unitsWithKcal) { unitsWithKcalList.Add($"{unit.Key}({unit.Value})"); ListItem matchedItem = lbDostupneJedinice.Items.FindByText(unit.Key); lbDostupneJedinice.Items.Remove(matchedItem); } lbDodaneJedinice.DataSource = unitsWithKcalList; lbDodaneJedinice.DataBind(); } else { FoodTableBind(); } } }
protected List <string> GetMeasuringUnitsWithKcal(int id) { unitsWithKcal = SQLProcedures.GetMeasuringUnitsForFoodID(id); unitsWithKcalList = new List <string>(); foreach (var unit in unitsWithKcal) { unitsWithKcalList.Add($"{unit.Key}({unit.Value})"); } return(unitsWithKcalList); }
//calculations for units and kcals private string CalculateUnitAmount(int foodID, double foodTypePortion) { List <string> formattedUnitList = new List <string>(); Dictionary <string, int> unitsWithKcal = SQLProcedures.GetMeasuringUnitsForFoodID(foodID); foreach (KeyValuePair <string, int> unit in unitsWithKcal) { double factor = Math.Round(foodTypePortion / unit.Value, 2); string formattedUnit; if (unit.Key == "100 grama") { formattedUnit = factor.ToString() + "x 100g"; } else { formattedUnit = factor.ToString() + unit.Key.ToLower(); } formattedUnitList.Add(formattedUnit); } return(string.Join(" / ", formattedUnitList)); }