【发布时间】:2013-08-02 14:09:43
【问题描述】:
我正在使用 ASP .NET 创建一个电子商务网站,并使用如下文字控件将产品从数据库动态添加到购物页面:
foreach (DataRow dr in dt.Rows)
{
string productName = dr["PRO_Name"].ToString();
string productPrice = dr["PRO_Price"].ToString();
string myUrl = "Handler.ashx?ProdID=" + dr["PRO_ID"].ToString();
string sProduct = @"<div class='four-shop columns isotope-item'><div class='shop-item'><figure><img src='" + myUrl + "'/><figcaption class='item-description'><h5>" + productName + "</h5><span>R" + productPrice + "</span></figcaption></figure></div></div>";
productPanel.Controls.Add(new LiteralControl(sProduct));
}
我想知道如何在我的文字控件中添加一个带有点击事件的按钮。我已经创建了这个按钮,但不能将它添加到文字控件,因为文字控件只显示标记而不是服务器端控件。如何在从数据库中添加到购物页面的每个产品中动态添加此按钮。
Button button = new Button();
button.Text = "Add to cart";
button.Click += button_Click;
【问题讨论】:
-
使用普通控件(例如 div 面板)而不是将其全部放在文字控件中,然后您可以将按钮添加到控件树中的任何位置。