我正在为学校的网站工作。我正在用db产品开店。我在foreach循环中为我的所有产品制作了一个按钮。对于每个按钮,我需要在该特定产品的char中保存数据。我做错事情了。请帮帮我。
public partial class OrderFood : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FillPage();
}
public void FillPage()
{
ProductModel productModel = new ProductModel();
List<Product> products = productModel.GetAllProducts();
if (products != null)
{
foreach (Product product in products)
{
Panel OFood = new Panel();
Image img = new Image();
Label lblName = new Label();
Label lblDetail = new Label();
Label lblPrice = new Label();
Button btn = new Button ();
DropDownList ddamount = new DropDownList();
//amount
int[] amount = Enumerable.Range(1, 20).ToArray();
ddamount.DataSource = amount;
ddamount.AppendDataBoundItems = true;
ddamount.DataBind();
btn.ID = "s";
img.ImageUrl = "~/AdminManagement/FoodImages/" + product.Image;
img.CssClass = "img-rounded3";
ddamount.CssClass = "form-control";
lblName.Text = product.Name;
btn.CssClass = "btn-success btn";
btn.Text = "comanda mancare";
lblDetail.Text = product.Desctiption;
lblPrice.Text = "Lei: " + product.Price;
OFood.Controls.Add(new Literal { Text = "<div class='col-xs-6 col-sm-3'>" });
OFood.Controls.Add(img);
OFood.Controls.Add(new Literal { Text = "<br />" });
OFood.Controls.Add(new Literal { Text = "<h3> " });
OFood.Controls.Add(lblName);
OFood.Controls.Add(new Literal { Text = "</h3>" });
OFood.Controls.Add(new Literal { Text = "<br />" });
OFood.Controls.Add(lblDetail);
OFood.Controls.Add(new Literal { Text = "<br />" });
OFood.Controls.Add(new Literal { Text = "<h4> " });
OFood.Controls.Add(lblPrice);
OFood.Controls.Add(new Literal { Text = "</h4> " });
OFood.Controls.Add(ddamount);
OFood.Controls.Add(btn);
OFood.Controls.Add(new Literal { Text = "</div>" });
// add dinamyc panlels to static parent panel
OFoodP.Controls.Add(OFood);
btn.Click +=delegate
{
if (!string.IsNullOrWhiteSpace(Request.QueryString["id"]))
{
string clientId = "-1";
int id = Convert.ToInt32(Request.QueryString["id"]);
int AMount = Convert.ToInt32(ddamount.SelectedValue);
Cart cart = new Cart()
{
Amount = AMount,
ClientID = clientId,
DatePurchased = DateTime.Now,
IsInCart = true,
ProductID = id,
};
CartModel model = new CartModel();
lblResult.Text = model.InsetCart(cart);
}
};
}
}
else
{
//no products found
OFoodP.Controls.Add(new Literal { Text = "no food found" });
}
}
}