Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > DataBinding ListItemCollection to DropDownList

Reply
Thread Tools

DataBinding ListItemCollection to DropDownList

 
 
=?Utf-8?B?UmljaGFyZEg=?=
Guest
Posts: n/a
 
      04-12-2006
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:
<aspropDownList runat="server" ID="test2">
<asp:ListItem Text="1" />
<asp:ListItem Selected="True" Text="2" />
</aspropDownList>

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
 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
Guest
Posts: n/a
 
      04-12-2006
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:
> <aspropDownList runat="server" ID="test2">
> <asp:ListItem Text="1" />
> <asp:ListItem Selected="True" Text="2" />
> </aspropDownList>
>
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ListItemCollection Sort Alphabetical =?Utf-8?B?YmVub2l0?= ASP .Net 4 11-03-2005 04:56 PM
databinding a Dropdownlist to another dropdownlist tshad ASP .Net 8 10-19-2005 10:00 PM
Problem with binding ListItemCollection with DropDownList =?Utf-8?B?UkFNQURV?= ASP .Net 2 05-16-2004 09:31 AM
Problem with binding DropDownList with ListItemCollection =?Utf-8?B?UkFNQURV?= ASP .Net 0 05-16-2004 06:51 AM
ListItemCollection Value in DropDownList - MIA! Kurt Mang ASP .Net 3 08-18-2003 08:10 PM



Advertisments