将ASP DropDownList动态添加到li控件中

时间:2015-01-19 12:32:54

标签: c# html asp.net

我有以下代码:

        HtmlGenericControl li = new HtmlGenericControl("li");
        ULRouting.Controls.Add(li);

        HtmlGenericControl anchor = new HtmlGenericControl("a");
        li.Attributes.Add("myCustomIDAtribute", "11");
        anchor.InnerText = "TabX";

        li.Controls.Add(anchor);

我正在尝试找出当你动态创建li时如何向li控件添加一个下拉框,即:

 <asp:DropDownList ID="cmbRoutingStage_1" runat="server" DataSourceID="sqlDBStages" DataTextField="stages" DataValueField="StageRefID"></asp:DropDownList>

1 个答案:

答案 0 :(得分:0)

试试这个: -

DropDownList ddl = new DropDownList();
ddl.ID = "cmbRoutingStage_1";
ddl.DataSourceID = "sqlDBStages";
ddl.DataTextField = "stages";
ddl.DataValueField = "StageRefID";
li.Controls.Add(ddl);

作为旁注,您可以使用HtmlAnchor类来生成锚标记: -

HtmlAnchor anchor = new HtmlAnchor();