Tuesday, October 9, 2007

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...


Share:

2 comments:

  1. Thanx a lot. This is very useful and has a major impact on user frinedliness

    ReplyDelete
    Replies
    1. ddl1.DataSource = SQLData();
      ddl1.DataTextField = "Name";
      ddl1.DataValueField = "ID";
      ddl1.DataBind();
      ddl1.Items.Insert(0, new ListItem("--select all--", "0"));

      Delete

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews