首页 > 解决方案 > 有什么方法可以根据数据库中的记录复制带有 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 !" });
    }
}

标签: c#asp.net

解决方案


推荐阅读