forum

Home / DeveloperSection / Forums / Insert a ListItem to a DropDownList

Insert a ListItem to a DropDownList

Lillian Martin 1784 27-May-2015

I'm trying to add an ListItem at the beginning of my dropdownlist. I don't know why this code is not working. It only adds the database information. I created a class that inherits from DropDownList in order to customize my own dropdownlist. 

public class MyDropDownList : DropDownList
{
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.BindItems();
        }
 
        private void BindItems()
        {
            this.Items.Clear();
            this.DataSource = this.GetData();
            this.DataValueField = "CompositePK";
            this.DataTextField = "Description";
            this.DataBind();
            this.Items.Insert(0, new ListItem("-Select-", "-1"));
        }
}


Updated on 27-May-2015

Can you answer this question?


Answer

1 Answers

Liked By