How to insert a item in databound dropdownlist at nth position

Hello guys,

Sorry for not posting anything for long time. Actually i was busy about changing my job.

We have use the dropdownlist with millians of time some developer still does not know that how to insert the listitem at particular level.

Suppose your dropdownlist is having 5 items and it isbound with some datasource and you want to insert "--Select---" on the top of the order. You can do it easily with following code.

DropDownList1.Items.Insert(0, new ListItem("--Select---", "0"));

So "--Select--" will be added at the top of the dropdownlist box. You can add it at any index as you want via specifing index as first argument.

If you are using the asp.net 2.0 then it is having one property
AppendDataBoundItems set it to true and write directly a new list item in html code. It will work.

Happy Programing...

 

Published Monday, January 28, 2008 8:07 PM by Jalpesh P. Vadgama
Filed under: , ,

Comments

# re: How to insert a item in databound dropdownlist at nth position

Thursday, July 16, 2009 1:05 AM by Abdul wakeel

To Insert in the 0 position

ddl.items.insert(0,new listitem("all",0)

# re: How to insert a item in databound dropdownlist at nth position

Monday, January 30, 2012 5:43 AM by Ali

Thanks a lot,it was very useful.

# re: How to insert a item in databound dropdownlist at nth position

Wednesday, May 09, 2012 1:01 AM by Bhavya

How to add an item, if dropdownlist is empty?

# re: How to insert a item in databound dropdownlist at nth position

Wednesday, May 09, 2012 2:08 AM by Jalpesh P. Vadgama

@Bhvya- You need to use ddl.items.insert(0,new listitem("all",0)

# re: How to insert a item in databound dropdownlist at nth position

Monday, June 04, 2012 11:31 PM by Sandip

Hi,

I Insert the default value in the drop down but it not show me the 1 value record which is fetched from database. The values are the ID of the table rows.

Thanks in advance

# re: How to insert a item in databound dropdownlist at nth position

Tuesday, September 11, 2012 6:08 AM by Dave Smith

Thats, clear and concise

# re: How to insert a item in databound dropdownlist at nth position

Monday, September 17, 2012 6:55 AM by Jalpesh P. Vadgama

@Thanks Dave