【问题标题】:Literal Control with an asp button带有 asp 按钮的文字控制
【发布时间】: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 面板)而不是将其全部放在文字控件中,然后您可以将按钮添加到控件树中的任何位置。

标签: c# asp.net


【解决方案1】:

试试这个方法。

Table tb = new Table();

for (int i = 1; i <= 10; i++)
        {
            TableRow tr = new TableRow();
            TableCell td = new TableCell();
            Button bt = new Button();
            bt.ID = i.ToString();
            bt.Text = "Button " + i.ToString();
            bt.Click += new EventHandler(btn_Click);
            td.Controls.Add(bt);
            tr.Controls.Add(td);
            tb.Controls.Add(tr);
        }

dvTest.Controls.Add(tb);

【讨论】:

    【解决方案2】:

    而不是使用文字控制。您可以直接将其添加到页面控件中,如果您仍然热衷于使用容器控件,请使用面板控件。

    问候,

    【讨论】:

      【解决方案3】:

      我可以建议为您的产品构建一个用户控件。构建一个控件,例如:图像控件、用于描述的标签控件、用于价格等的标签以及用于添加到购物车的按钮等。

      通过这种方式,您可以为正在显示的每个产品动态创建控件,在添加按钮中添加代码,并通过代码为每个产品设置产品属性,如图像、描述等。

      用户控件将在页面上呈现为普通 HTML,但它允许您在代码中使用每个产品的“控件”/对象。

      【讨论】:

        【解决方案4】:

        与其做这样的动态,为什么不使用中继器。这将使您免于很多麻烦:

        <asp:Repeater id="repeat" runat="server">
         <ItemTemplate>
               <div class='four-shop columns isotope-item'><div class='shop-item'><figure><img src='<%#string.Format("Handler.ashx?ProdID={0}",Eval("PRO_ID")) %>'/><figcaption class='item-description'><h5><%#Eval("PRO_Name")%> </h5><span><%#Eval("PRO_Name")%> </span></figcaption></figure></div></div>      
               <asp:button id="btnAdd" runat="server" Text="Add to Cart" OnClick="btnAdd_Click" />
         </ItemTemplate>
        </asp:Repeater>
        

        在后面的代码中,您定义了按钮单击的处理程序:

        protected void btnAdd_Click(object sender, EventArgs e)
        {
             //handle add for the product here
        }
        

        【讨论】:

          【解决方案5】:

          这样使用

          Button button = new Button();
          button.Text = "Add to cart";
          button.Click += button_Click;
          productPanel.Controls.Add(button );
          

          在添加文字控件之后。如果您希望它位于 LiteralControl(即 sProduct)之间的某个位置,则必须将其分成两部分。

          这样,

          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>";
                  productPanel.Controls.Add(new LiteralControl(sProduct));
              Button button = new Button();
              button.Text = "Add to cart";
              button.Click += button_Click;
              productPanel.Controls.Add(button );
              productPanel.Controls.Add(new LiteralControl("</div></div>"));
          

          但是,不建议像这样使用 LiteralControl。在这种情况下,您可以使用 HtmlGenericControl。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-06-12
            • 2012-01-06
            • 1970-01-01
            • 2018-08-15
            • 1970-01-01
            • 2012-09-13
            • 1970-01-01
            • 2014-08-08
            相关资源
            最近更新 更多