c# - 将控件添加到表格内的innerhtml div

标签 c# asp.net innerhtml

在 div 内,我尝试从代码隐藏添加一个表格。 我需要向该表添加 2 个控件,因此为了做到这一点,我将代码放入 Page_Init() 中,但它不会添加控件,所以我应该做什么来解决这个问题还是应该走完全不同的路。

我的代码:

if ((List<ShoppingCart>)Session["shoppin"] != null)
        {
            string cartcontent = "";
            TextBox txtbox = new TextBox();
            txtbox.TextChanged +=new EventHandler(txtbox_TextChanged);
            txtbox.CssClass = "carttextbox";
            Button btn = new Button();
            btn.Click +=new EventHandler(btn_Click);
            btn.Text = "Remove";
            btn.CssClass = "cartbtn";
            maincontent.Controls.Add(txtbox);
            maincontent.Controls.Add(btn);

            foreach (ShoppingCart r in (List<ShoppingCart>)Session["shoppin"])
            {
                cartcontent = cartcontent + "<tr class='row'>" +
                    "<td class='cart_prods'>" +
                    "<table>" +
                    "<tr>" +
                    "<td colspan='2'><b>" + r.ProductName + "</b></td>" +
                    "</tr>" +
                    "<tr>" +
                    "<td align='center' style='width:100%'> <img src='" + r.ProductImage + "' alt='' style='max-height:150px' /></td>" +
                    "<td><div class='.cart_products_options'><i>" + r.ProductOptions + "</i></div></td>" +
                    "</tr>" +
                    "</table>" +
                    "</td>" +
                    "<td class='cart_update' style='border-width: 0px 1px 1px 0px;'>" + txtbox + btn + "</td>" + 
                    "<td class='cart_price' style='border-width: 0px 0px 1px;'><span class='ProductSpecialPrice'> $"+ r.ProductPrice + "</span></td>"
                    +"</tr>";
                double pricetotal = 0;
                pricetotal = pricetotal + r.ProductPrice;
            }

            maincontent.InnerHtml = "<table width='90%' style='margin:0 auto; margin-top:50px;' class='cart'>" +
                "<tr align='center'>" +
                    "<th class='th1'><b>Product(s)</b></th>" +
                    "<th><b>Qty</b></th>" +
                    "<th class='th3'><b>Total</b></th>" +
                "</tr>" + cartcontent +
                "<tr class='cart_total'>" +
                    "<td></td>" +
                    "<td style='border-width: 1px 1px 0px 0px; text-align:right;'>Sub-Total</td>" +
                    "<td></td>" +
                "</tr>" +
            "</table>";
        }

最佳答案

以下方法应该更简单,并且还可以避免动态添加控件:

  • 在页面上创建转发器。
  • 设置 HeaderTemplate 和 FooterTemplate,使其包含开始表格标记(如果是页脚则为结束标记)以及表格页眉和页脚的标记。
  • 设置 ItemTemplate,使其与您在 cartcontent 中构建的标记匹配字符串。将包含购物车数据的控件添加到 ItemTemplate 中,并使用数据绑定(bind)设置文本。
  • 将中继器绑定(bind)到 List<ShoppingCart>如果有条目,否则将 Visible 设置为 false。

有关中继器控件的详细信息,请参阅此 link 。它还包含各种教程的链接。

更新:
为了在不使用 DataBinding 的情况下动态设置一行中的值,例如要在页脚中设置总计,请执行以下操作:

  • 向页脚添加标签,为其分配 ID 并设置 runat="server" .
  • 将处理程序添加到 ItemDataBound中继器的事件。
  • 在处理程序中,检查绑定(bind)项目的类型。如果是页脚,找到Label控件并为其赋值。

private void Rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Footer)
    {
        var lbl = (Label)e.Item.FindControl("LabelId");
        if (lbl != null)
            lbl.Text = "123.45";
    }
}

关于c# - 将控件添加到表格内的innerhtml div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21390326/

相关文章:

c# - 为什么需要双冒号 (::) 运算符来解决 namespace 冲突?

c# - Orion 上下文代理 : check subscription expiration?

c# - 带有复选框的 ASP.NET 更新面板 - 无法正常工作

asp.net - 如何在 .Net 中测试字符串(网页)的标记有效性?

javascript - 当从函数返回内容时,将新创建的 TD 元素上的 .innerHTML 设置为空白

Javascript 使用 document.write 写入 innerHtml 的值

jquery - 定位标签之间的文本?

c# - WP7 - 从 CSV 文件读取?或者如何处理数据?

c# - 向后导航不会释放内存

javascript - 从 javascript 验证文本框在任何浏览器中都不起作用,但在 IE 中有效