Insert Item at top of the DropDownList in ASP.net Codebehind

ASP.NET Comments Off on Insert Item at top of the DropDownList in ASP.net Codebehind

Here is the snippet, for demo i am pulling the List of States and its ID from the database

 

ddl_State.DataSource = GetAllStates();
ddl_State.DataTextField = “Title”;
ddl_State.DataValueField = “StateID”;
ddl_State.DataBind();

 

ddl_State.Items.Insert(0, new ListItem(“Select State”, “”)); // I am inserting¬†Empty Value at the top of the dropdownlist

ddl_State.SelectedIndex = 0; // I am selecting the firs item

 

If you want to add an item to the dropdownlist then use Add method


DropDownList1.Items.Add(new ListItem(“Tamil Nadu”,”tamil-nadu”));



Author

Search

Back to Top