DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

This is my first post in my new blog.  As I write this I have nestled down for an all night work session with a HUGE cafeteria of REALLY REALLY strong coffee lol. I would like to say that I am very please Joe Stagner has invited people into these asp.net blogs.

I want to make this first one a really quick handy tip and it is to do with the DropDownList control.  I see so many questions and get asked so many times about this. When you have a databound DropDownList you will probably require an option which asks for the users selection i.e. a static option like -Please Select- .

A lot of the time people will put into a conditonal block where not postback to create a static item.  What you can do is insert an item to your drop down list declaratively and add the attribute AppendDataBoundItems="True" to your drop down list.

What happens now is simply that instead of replacing any core items that were hardcoded, it does as the paramater suggests and appends additional data bound items onto the drop down list.

Cheers,

 

Andrew

Published Wednesday, January 23, 2008 10:37 PM by REA_ANDREW

Comments

# re: DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

Wednesday, February 6, 2008 10:04 AM by Wayne

You need to show the exact code for this. 'insert an item to your drop down list declaratively' means nothing to me.

# re: DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

Tuesday, February 19, 2008 7:04 AM by REA_ANDREW

Hi Wayne, apologies for not being clear there. My meaning was to simply add a default item in the mark up like so:

       <asp:DropDownList ID="ddl1" runat="server" AppendDataBoundItems="true">

           <asp:ListItem Value="0" Text="Please Select"></asp:ListItem>

       </asp:DropDownList>

Cheers

Andrew

# re: DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

Tuesday, April 8, 2008 8:14 AM by yes_no

Hi Andrew,

It seems you are familar with the dropdownlist..Great job done..

I have another problem regarding dropdownlist select item.. I have dynamically bind the data into dropdownlist from oracle database table and I wanted to get the item selected but when I select and clicked the button it is selected the first item only... how can I select the selected item to be printed..

Thanks in advance

# re: DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

Tuesday, April 8, 2008 8:14 AM by yes_no

Hi Andrew,

It seems you are familar with the dropdownlist..Great job done..

I have another problem regarding dropdownlist select item.. I have dynamically bind the data into dropdownlist from oracle database table and I wanted to get the item selected but when I select and clicked the button it is selected the first item only... how can I select the selected item to be printed..

Thanks in advance

# re: DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

Monday, April 21, 2008 7:04 AM by REA_ANDREW

Hi,

I can recommend two options here.  Both are methods of the DroDpwnList:

1. YourDropDownList.Items.FindByText("Text").Selected = true;

2. YourDropDownList.Items.FindByValue("Value").Selected = true;

I would wrap these in a try catch block, incase the item's text you are searching for cannot be found, as in that case an Exception will be thrown.

Hope this helps.

Andrew

# re: DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

Friday, June 6, 2008 7:45 PM by Anurag

This doesn't work for dropdownlists in framework 1.1 (visual studio 2003). The project wont compile with error "AppendDataBounds" is not a member of System.web.ui.webControls.DropDownList. Whats the workaround?

Anurag

# re: DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

Sunday, June 8, 2008 12:51 AM by Anurag

ok, I asked the question and I created a workaround - for those (like me) who are still stuck with framework 1.1 (visual studio 2003).

Use the following code (Pass the dropdown as the first parameter):

Public Sub LoadCategoryDropdown(ByRef CatDrpDwn As System.Web.UI.WebControls.DropDownList, ByVal dvCats As DataView, _

                                   ByVal TextField As String, ByVal ValueField As String, _

                                   ByVal FirstItemText As String, ByVal FirstItemValue As String)

       Dim LI As ListItem, i As Integer

       'add first item

       LI = New ListItem(FirstItemText, FirstItemValue)

       LI.Selected = True

       CatDrpDwn.Items.Add(LI)

       'add

       If Not dvCats Is Nothing Then

           For i = 0 To dvCats.Count - 1

               LI = New ListItem((dvCats(i)(TextField)), dvCats(i)(ValueField))

               CatDrpDwn.Items.Add(LI)

           Next

       End If

   End Sub

If anyone has a better idea, please post it here.

# re: DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

Friday, August 22, 2008 5:29 AM by Miguel

What about the repeated items when you Postback the page several times?

# re: DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

Friday, August 22, 2008 5:50 AM by Miguel

Ups! Forget about my previous comment! I've found the problem! Was an extra Databind() call lost in the code

# re: DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

Tuesday, October 28, 2008 5:15 PM by Francis

Hi Andrew ... nice post .. I did exacly what you have said and it is working great .. solved my problem :)

There's a little thing strange though ... I must click two time on my dropdownlist to get the second one populated... Did you ever encounter that kind of trouble ?

# re: DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

Tuesday, October 28, 2008 5:27 PM by REA_ANDREW

It sounds like you may be databinding in the wrong event handler.  OR, you have not contained the bind inside the !IsPostBack conditional block?  I cannot be too sure though without looking at your code. :-) Good luck and thanks for the post

# re: DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

Saturday, February 14, 2009 9:19 AM by Gene Hammond

Thank you very much This is exactly what I needed

# re: DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

Saturday, May 23, 2009 6:54 AM by Arjun Pathak

hi there,

Setting AppendDataBoundItems=true solves the problem of keeping --Please Select-- as the first item of the list, But it causes next problem that on reloading of the page, it keeps on maintaining items which are previously populated. This is a problem when the same dropdownlist is conditionally populated. i.e. on RadioButton_click or on other dropdown_selectedindexchanged etc. so is there any solution ?

As a chance, if we keep AppendDataBoundItems=true in if(!IsPostback){} segment, then it works for once only... but a real-time end user of a webpage would see every options available on page and so every time the page may post back...leading to populating the dropdown again and then the first item --select-- cannot be maintained.

# re: DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

Saturday, June 13, 2009 2:37 PM by KevinB

In your SQL use SELECT DISTINCT this worked for me

# re: DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

Thursday, September 10, 2009 6:24 AM by Javad

hi

thanks for this post, i had this problem(hard coding on OnDataBound event of DropDown list).

again thanks a lot.

# re: DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

Wednesday, May 26, 2010 6:34 AM by DDA

Hi

Is there a way around this? :

records not opening due to data in a fields not being the same as whats in a databound list for that field on the webpage?

Thanks

# re: DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

Tuesday, May 17, 2011 11:04 AM by Jen

Using VS2008 and need help with databound dropdown list.

When dropdown 1 = 2 I need my databound dropdown to have a hardcoded date.  If anyone has any ideas many thanks in advance. :)

# re: DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

Tuesday, October 11, 2011 9:57 AM by Ash

Hi Andrew,

I'm new to this.

I have created SQL database called Contacts it has 3 tables

1- Contacts table with contact information that has countries and departments

2- Departments table which includes list department names

3- Countries which in includes list of countries

Using Microsoft Visual Web Developer i created a web form to update the contacts table in my database. In the form there are 2 field which i want them dropdowns

Countries and Departments. so i have created thoes drop down and they work fine by linking them to the relevant lists from my database.

My problem is how to return the value i select from the drop down to the main table (Contacts) in the data base...Exmple

I selected from the drop down

- country: US

- Department: Finance

How can i post these values in the country and department fields in the contacts table.

Appreciate your assistance.

Thanks

Ash

# re: DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

Thursday, July 12, 2012 11:42 AM by Kay Wrobel

I just ran across this post and am aware it is an older post. The problem is of course still a valid one. I'd like to offer a solution:

1.) Add the first ListItem declaratively as discussed above.

2.) Do NOT set AppendDataBoundItems="true", but to "false" (default).

3.) In your code-behind file (here VB), add handlers for DataBinding and DataBound and a private variable on to of the page. Here's a abbreviated example:

   Private savedItem As ListItem

   Protected Sub DropDownList1_DataBinding(sender As Object, e As EventArgs) Handles ddl1.DataBinding

       'Save the first item to add back later

       savedItem = Me.ddl1.Items(0)

   End Sub

   Protected Sub DropDownList1_DataBound(sender As Object, e As EventArgs) Handles ddl1.DataBound

       'Add the first item back to the list at first position

       Me.ddl1.Items.Insert(0, savedItem)

   End Sub

I just tried it and it works.

# re: DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

Saturday, February 2, 2013 12:43 PM by zlOJMVbsAgdDdYCD

KbYNz1 Looking forward to reading more. Great article. Want more.

# re: DropDownList AppendDataBoundItems (A quick starter, a tip which many will know but for those who do not...)

Saturday, June 1, 2013 6:46 AM by Kinder

At any time, 50, 000 lovemaking predators online.

Invisibility - in Keylogger software reviews, the most difficult thing is not

how to make money but the way of managing people.

The kind of program that works invisibly.