我有一个列表项,其中显示一个选项卡和一个链接按钮:

<li runat="server" id="liActivityInvoices"><a href="#tabActivityInvoices">Invoices</a><asp:LinkButton runat="server" ID="btnLoadInvoice" OnClick="btnInvoiceActivity_Click"  CssClass="btnListSmall" Width="90px"></asp:LinkButton></li>

javascript - 从InnerHtml添加链接按钮-LMLPHP
在后面的代码中,它搜索发票并获取总收入。然后更新列表项以在选项卡中显示货币和收入:
liActivityInvoices.InnerHtml = "<a href=\"#tabActivityInvoices\">Invoices (" + Company.Current.DefaultCurrency + ": " + Convert.ToDecimal(ds.Tables[1].Rows[0]["TotalRev"]) + ")</a> <asp:LinkButton runat=\"server\" ID=\"btnLoadInvoice\" OnClick=\"btnInvoiceActivity_Click\"  CssClass=\"btnListSmall\" Width=\"90px\"></asp:LinkButton>";

javascript - 从InnerHtml添加链接按钮-LMLPHP
a href部分显示,但链接按钮消失。如何使用InnerHtml显示链接按钮?

最佳答案

LinkButton是服务器控件。不能使用内部html创建服务器控件。您将不得不将控件作为子控件添加到liactivityvoices。

var anchor = new LiteralControl("<a href=\"#tabActivityInvoices\">Invoices (" + Company.Current.DefaultCurrency + ": " + Convert.ToDecimal(ds.Tables[1].Rows[0]["TotalRev"]) + ")</a>");
var lnkButton = new LinkButton
{
    ID = "btnLoadInvoice",
    CssClass = "btnListSmall",
    Width = new Unit("90px")
};
lnkButton.Click += btnInvoiceActivity_Click;
liActivityInvoices.Controls.Add(anchor);
liActivityInvoices.Controls.Add(lnkButton);

10-04 16:37