c# - 有什么方法可以根据数据库中的记录复制带有 Panel 的部分标签
问题描述
我是 C# 和 ASP.NET 的新手,我正在使用 Visual Studio 2017。我创建了一个带有 SQL 数据库和实体框架的网上商店。我在 -section- 标记内的表单中有一个面板,可将数据库中的所有产品检索到面板中。我想复制这个-section-标签,包括每种产品类型的面板。我已经创建了按类型获取产品的方法。我想在我的表单中为每个产品模型提供 -section- 产品。
这是我的代码
HTML
<section class="product-carousel">
<!-- Heading Starts -->
<h2 class="product-head">Latest Products</h2>
<!-- Heading Ends -->
<!-- Products Row Starts -->
<div class="row">
<asp:Panel ID="pnlProducts1" runat="server"
CssClass="IndexPanelProduct"></asp:Panel>
</div>
<!-- Products Row Ends -->
</section>
<!-- Latest Products Ends -->
代码背后
private void FillPage(int type)
{
ProductModel productModel = new ProductModel();
List<Product> products = productModel.GetProductByType(type);
if (products != null)
{
foreach (Product product in products)
{
Panel productPanel1 = new Panel();
ImageButton imageButton = new ImageButton();
Label lblShortDesc = new Label();
Label lblbuysCount = new Label();
Label lblNewPrice = new Label();
Label lblOldPrice = new Label();
imageButton.ImageUrl = "~/Images/product-images/" + product.Image;
imageButton.CssClass = "productImage";
imageButton.PostBackUrl = "~/Product_page.aspx?id=" + product.Id;
lblShortDesc.Text = product.Name;
lblShortDesc.CssClass = "mainProductDesc";
lblbuysCount.Text = "Buys 120 +";
lblbuysCount.CssClass = "mainProductBuys";
lblNewPrice.Text = "Only for: $ " + product.NewPrice;
lblNewPrice.CssClass = "mainProductprice";
productPanel1.Controls.Add(imageButton);
productPanel1.Controls.Add(lblShortDesc);
productPanel1.Controls.Add(new Literal { Text = "<br />" });
productPanel1.Controls.Add(lblNewPrice);
productPanel1.Controls.Add(new Literal { Text = "<br />" });
productPanel1.Controls.Add(lblNewPrice);
productPanel1.Controls.Add(new Literal { Text = "<br />" });
productPanel1.Controls.Add(lblbuysCount);
pnlProducts1.Controls.Add(productPanel1);
}
}
else
{
pnlProducts1.Controls.Add(new Literal { Text = "No Product Found !" });
}
}
解决方案
推荐阅读
- r - 在 R 中使用 rgl 包测量不规则物体的体积
- django - Django在多个字段中过滤__
- actionscript-3 - 检测字符串中的 URL,并仅使 URL 可点击,该字符串中没有其他内容
- java - 否则,如果未选中单选按钮时循环没有停止,则下一个活动仍然打开
- python - 当我单击带有 selenium 的搜索按钮时,网站会重新加载
- c# - 希望从两个不同层次的嵌套列表中获取信息
- android - Android 计费:更改订阅价格
- r - 条形图中误差线上方的统计字母在 R 的导出图中无法正确显示
- github-actions - 在 Github Actions 中运行 Node.js API
- css - 尽管使用了盒子大小,边距仍被添加到总宽度中