This is something that has been bugging me for a while.
I have a list of categories stored in a SQL Server table. I want to bind them to a drop down list. But I also want to add one additional option to the top of the list that simply says "Include All Categories" with an index of 0.
What is the simplest way to do this? Can it be done non-programmatically in the page design?
before doing the data binding from the data base do dropdownlist.items.add("Include All Categories"). The databind my clear out any items already added if that is the case you might be better off looping through the data. So you would need to do dropdownlist.items.add("Include
All Categories") followed by a loop that adds each item from the DB. So rather than databinding you might have to read from the DB.
TonyLoco23
Member
79 Points
57 Posts
How to add one additional option to the start of a databound dropdown list?
Apr 30, 2012 05:33 PM|LINK
This is something that has been bugging me for a while.
I have a list of categories stored in a SQL Server table. I want to bind them to a drop down list. But I also want to add one additional option to the top of the list that simply says "Include All Categories" with an index of 0.
What is the simplest way to do this? Can it be done non-programmatically in the page design?
dotnetdev201...
Member
319 Points
79 Posts
Re: How to add one additional option to the start of a databound dropdown list?
Apr 30, 2012 05:42 PM|LINK
before doing the data binding from the data base do dropdownlist.items.add("Include All Categories"). The databind my clear out any items already added if that is the case you might be better off looping through the data. So you would need to do dropdownlist.items.add("Include All Categories") followed by a loop that adds each item from the DB. So rather than databinding you might have to read from the DB.
do while dbReader.read
dropdownlist.items.add(dbReader.getValue(i)
i += 1
loop
texx
Contributor
2412 Points
415 Posts
Re: How to add one additional option to the start of a databound dropdown list?
Apr 30, 2012 06:30 PM|LINK
You can leave the dropdownlist databound however you need, but after it is databound you can call
dropdownlist.items.insert(0, new ListItem("All items"));
See article from 4guysfromrolla
http://www.4guysfromrolla.com/webtech/073101-1.shtml
dotnetdev201...
Member
319 Points
79 Posts
Re: How to add one additional option to the start of a databound dropdown list?
Apr 30, 2012 07:13 PM|LINK
o snap, i forgot about that. Texx's post should work for you.