C# 将控件添加到表内的innerhtml div

C# 将控件添加到表内的innerhtml div,c#,asp.net,innerhtml,C#,Asp.net,Innerhtml,在一个div中,我试图从codebeard添加一个表。 我需要在此表中添加2个控件,因此为了做到这一点,我将代码放在页面_Init() 我的代码: if ((List<ShoppingCart>)Session["shoppin"] != null) { string cartcontent = ""; TextBox txtbox = new TextBox(); txtbox.TextChan

在一个div中,我试图从codebeard添加一个表。 我需要在此表中添加2个控件,因此为了做到这一点,我将代码放在
页面_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>";
        }
if((列表)会话[“shoppin”]!=null)
{
字符串内容=”;
TextBox txtbox=新建TextBox();
txtbox.TextChanged+=新事件处理程序(txtbox\u TextChanged);
txtbox.CssClass=“cartextbox”;
按钮btn=新按钮();
btn.Click+=新建事件处理程序(btn\u Click);
btn.Text=“删除”;
btn.CssClass=“cartbtn”;
maincontent.Controls.Add(txtbox);
maincontent.Controls.Add(btn);
foreach(在(列表)会话[“shoppin”]中的ShoppingCart r)
{
cartcontent=cartcontent+“”+
"" +
"" +
"" +
“”+r.ProductName+“”+
"" +
"" +
" " +
“”+r.ProductOptions+“”+
"" +
"" +
"" +
“”+txtbox+btn+“”+
“$”+r.ProductPrice+”“
+"";
双倍价格合计=0;
pricetotal=pricetotal+r.ProductPrice;
}
maincontent.InnerHtml=“”+
"" +
“产品”+
“数量”+
“总计”+
“+cartcontent”+
"" +
"" +
“小计”+
"" +
"" +
"";
}

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

  • 在页面上创建一个转发器
  • 设置HeaderTemplate和FooterTemplate,使其包含开始表标记(如果是页脚,则为结束标记)以及表的页眉和页脚标记
  • 设置ItemTemplate,使其与您在
    cartcontent
    字符串中构建的标记相匹配。将包含购物车数据的控件添加到ItemTemplate,并使用数据绑定设置文本
  • 如果有条目,将中继器绑定到
    列表
    ,否则设置为Visible为false
有关中继器控件的详细信息,请参见此。它还包含指向各种教程的链接

更新:
要在不使用数据绑定的情况下动态设置行中的值,例如要在页脚中设置总计,请执行以下操作:

  • 向页脚添加标签,为其分配ID并设置
    runat=“server”
  • 向中继器的事件添加处理程序
  • 在处理程序中,检查绑定的项的类型。如果是页脚,则查找标签控件并为其指定值


成功了。唯一不需要的是,我需要从代码后面向页脚添加一个值,但我无法获取我放置的divthere@user3025852:很高兴听到它起作用了。我已经更新了页脚中的样本总数。
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";
    }
}