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 06, 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 08, 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 08, 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 06, 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 08, 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.

Leave a Comment

(required) 
(required) 
(optional)
(required)