已经为此苦苦挣扎了一段时间,问题是我试图在我虚构的网上商店中用 1 个产品填充 1 个面板。我在互联网上找到了一些代码,但无法成功使其正常工作。
protected void Page_Load(object sender, EventArgs e)
{
string ConnectieString = WebConfigurationManager.ConnectionStrings["ConnectieStringWebshop"].ConnectionString;
SqlConnection OphalenKoffieConnectie = new SqlConnection(ConnectieString);
SqlCommand OphalenKoffieCommand = new SqlCommand();
SqlDataReader OphalenKoffieDataReader;
try
{
OphalenKoffieConnectie.Open();
OphalenKoffieCommand.Connection = OphalenKoffieConnectie;
OphalenKoffieCommand.CommandText = "SELECT * FROM tblArtikelen WHERE Categorie = Koffie";
OphalenKoffieDataReader = OphalenKoffieCommand.ExecuteReader();
OphalenKoffieDataReader.Read();
foreach (DataRow row in OphalenKoffieDataReader)
{
Panel productPanel = new Panel();
ImageButton imageButton = new ImageButton
{
ImageUrl = "~/ProductMedia/" + product.Image,
CssClass = "productImage",
PostBackUrl = string.Format("~/Pages/Product.aspx?id={0}", product.ID)
};
Label lblName = new Label
{
Text = product.Name,
CssClass = "productName"
};
Label lblPrice = new Label
{
Text = "£ " + product.Price,
CssClass = "productPrice"
};
pnlKoffie.Controls.Add(imageButton);
pnlKoffie.Controls.Add(new Literal { Text = "<br/>" });
pnlKoffie.Controls.Add(lblName);
pnlKoffie.Controls.Add(new Literal { Text = "<br/>" });
pnlKoffie.Controls.Add(lblPrice);
pnlKoffie.Controls.Add(productPanel);
}
}
catch (Exception OphalenKoffieFaal)
{
lblOphalenKoffieFaal.Text = OphalenKoffieFaal.ToString();
}
finally
{
OphalenKoffieConnectie.Close();
}
}
我希望它为它在具有特定类别的 SQL DB 中找到的每个产品创建一个面板。我已经在我的数据库中存储了图像的链接和图像名称,图像位于“~/ProductMedia/”中有人有任何提示或技巧来让它工作吗?