Pascal Bayer
Pascal Bayer

Reputation: 2613

aspx dropdown adding items how to get dropdownlist instance

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

Answers (2)

Muhammad Akhtar
Muhammad Akhtar

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

smukh
smukh

Reputation: 91

numDropDown.Items.Add(new ListItem("text", option.Value));

Upvotes: -1

Related Questions