Reputation: 2613
I want to add items to my drop down in function addItems. How to do that at runtime?
<asp:DropDownList ID="DropDownNum" runat="server" Width="50px" SelectedValue='<%#Bind("num")%>' OnLoad='addItems'>
</asp:DropDownList>
protected void addItems() {
...
foreach (NumOption option in ConfigManager.Config.NumOptions.Options)
{
numDropDown.Items.Add(option.Value);
}
}
edit: I need to get the instance of the DropDownList to call it via numDropDown, the adding itself is not the problem
Upvotes: 0
Views: 1329
Reputation: 52241
You can add items like...
numDropDown.Items.Add(new ListItem("Text", "Value"));
Edit: In reference your comments, you are unable to get the Control reference in your code class. You have to find the control in the particular container e.g.
DropDownList numDropDown = (DropDownList)Container.Item.FindControl("DropDownNum");
Note: where Container is the control in your dropdownlist
Upvotes: 2