Populate bootstrap dropdown using C#
asp.net, c#, twitter-bootstrap
Solution
It would probably be easier for you to add them using a literal, like this:
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;
}
Problem
How can I populate a bootstrap dropdown from codebehind? Right now I am trying this: ``` 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); } ``` This does add the items but it completely loses the bootstrap design on the `<li>`-items. Also, how do I know which value is `DataTextField` and which is `DataValueField`? Dropdown 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> ```