Insert Item at top of the DropDownList in ASP.net Codebehind
ASP.NET June 2, 2014 Comments Off on Insert Item at top of the DropDownList in ASP.net CodebehindHere is the snippet, for demo i am pulling the List of States and its ID from the database
[sc:tcbox ]
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
[sc:/tcbox ]
If you want to add an item to the dropdownlist then use Add method
[sc:tcbox ]
DropDownList1.Items.Add(new ListItem(“Tamil Nadu”,”tamil-nadu”));
[sc:/tcbox ]