如何从代码隐藏中填充引导下拉列表?现在我正在尝试这个:
HtmlGenericControl li;
for (int x = 3; x <= 10; x++)
{
li = new HtmlGenericControl("li");
li.Attributes.Add("class", "myItemClass");
li.InnerText = "Item " + x;
myList.Controls.Add(li);
}
这会添加项目,但它会完全失去<li>
- 项目上的引导程序设计。另外,我如何知道哪个值为DataTextField
,哪个为DataValueField
?
下拉html:
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true">
Dropdown
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1" id="myList" runat="server">
</ul>
</div>
答案 0 :(得分:4)
您可能更容易使用文字添加它们,如下所示:
Dropdown.aspx:
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true">
Dropdown
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1" id="myList" runat="server">
<asp:Literal runat="server" id="litDropDown"></asp:Literal>
</ul>
</div>
Dropdown.aspx.cs:
for (int x=3; x<10;x++)
{
string liText = "";
liText = liText + "<li role=\"presentation\">";
liText = liText + "<a role=\"menuitem\" tabindex=\"-1\" href=\"#\">";
liText = liText + "Item " + x;
liText = liText + "</a></li>";
litDropDown.Text = litDropDown.Text + liText;
}