473,396 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

DataBinding ListItemCollection to DropDownList

Hi,

I have a ListItemCollection that I bind to DropDownList:
ListItemCollection items = new ListItemCollection();
ListItem item;
item = new ListItem("Option 1", "1");
items.Add(item);
item = new ListItem("Option 2", "2");
item.Selected = true;
items.Add(item);
ddl1.DataSource = items;
ddl1.DataBind();

As you see the second ListItem is selected and should be selected in the
dropdownlist. But isn't!

However, if i use:
<asp:DropDownList runat="server" ID="test2">
<asp:ListItem Text="1" />
<asp:ListItem Selected="True" Text="2" />
</asp:DropDownList>

Everything works as expected.

Is there an explanation to this? And is there a possible solution? I guess
one could put the selected ListItem fist in the DropDownList but this would
mess up the option and isn't a good solution for me.

Thanks in advance

Richard
Apr 12 '06 #1
1 2934
ListControl.DataSource property will only pickup the DataTextFiled and
DataValueField from your collection.
http://msdn.microsoft.com/library/de...ourcetopic.asp

For that purpose you can use any collection that implements the ILIST
interface (i.e. you did not have to use a ListItemCollection). If you are
interested in composing the list items like you did, you should have used a
simple array of type ListItem like this:

ListItem[] items = new ListItem[2];
items[0] = new ListItem("Option 1", "1");
items[1] = new ListItem("Option 2", "2");
items[1].Selected = true;
ddl1.AddRange (items);
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"RichardH" wrote:
Hi,

I have a ListItemCollection that I bind to DropDownList:
ListItemCollection items = new ListItemCollection();
ListItem item;
item = new ListItem("Option 1", "1");
items.Add(item);
item = new ListItem("Option 2", "2");
item.Selected = true;
items.Add(item);
ddl1.DataSource = items;
ddl1.DataBind();

As you see the second ListItem is selected and should be selected in the
dropdownlist. But isn't!

However, if i use:
<asp:DropDownList runat="server" ID="test2">
<asp:ListItem Text="1" />
<asp:ListItem Selected="True" Text="2" />
</asp:DropDownList>

Everything works as expected.

Is there an explanation to this? And is there a possible solution? I guess
one could put the selected ListItem fist in the DropDownList but this would
mess up the option and isn't a good solution for me.

Thanks in advance

Richard

Apr 12 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gonçalo Boléo | last post by:
I'm creating a control that has a DropDownList in it. I wan't to expose in that control the ListItemCollection of the DropDown. I wan't that the exposed ListItemCollection appears in the Properties...
6
by: Kurt Mang | last post by:
Hi all -- I hate to yell "BUG", but can anyone explain why I cannot get a dropdown list / checkboxlist to recognise the Value property of a ListItemCollection (created at runtime) that I bind...
0
by: RAMADU | last post by:
Hi There is a ListItemCollection containing ListItems (of course), each of whose Value and Text properties have been set. The Value and Text's values are different i.e. ListItem.Text = "Name0"...
2
by: RAMADU | last post by:
Hi There is a ListItemCollection containing ListItems (of course), each of whose Value and Text properties have been set. The Value and Text's values are different i.e. ListItem.Text = "Name0"...
4
by: JV | last post by:
It's easy to databind a listbox or dropdownlist if all you want is to fill it with a list of values. There are plenty of examples in the online help. Unfortunately, real world applications...
1
by: Joe Gass | last post by:
I'd like to bind some xml to a dropdownlist <engines> <engine name="test1" id="1" /> <engine name="test2" id="2" /> </engines> If I do: ddlEngines.DataSource =...
1
by: CorporateCoder | last post by:
Hi, I am trying to bind the selected value of a databound dropdown box in a databound gridview control to the value being displayed in the template column the dropdown box has been added to. ...
0
by: John Smith | last post by:
This is what I am trying to do: <asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundColumn Visible="False" DataField="id" ReadOnly="True"...
0
by: John Smith | last post by:
This is what I am trying to do: <asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundColumn Visible="False" DataField="id" ReadOnly="True"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.