DataBinding ListItemCollection to DropDownList

Discussion in 'ASP .Net' started by =?Utf-8?B?UmljaGFyZEg=?=, Apr 12, 2006.

  1. 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
    =?Utf-8?B?UmljaGFyZEg=?=, Apr 12, 2006
    #1
    1. Advertising

  2. ListControl.DataSource property will only pickup the DataTextFiled and
    DataValueField from your collection.
    http://msdn.microsoft.com/library/d...ndowsformslistcontrolclassdatasourcetopic.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
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Apr 12, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kurt Mang
    Replies:
    3
    Views:
    3,965
    szabelin
    Aug 18, 2003
  2. =?Utf-8?B?UkFNQURV?=
    Replies:
    0
    Views:
    606
    =?Utf-8?B?UkFNQURV?=
    May 16, 2004
  3. =?Utf-8?B?UkFNQURV?=
    Replies:
    2
    Views:
    8,344
    Ashish M Bhonkiya
    May 16, 2004
  4. =?Utf-8?B?YmVub2l0?=

    ListItemCollection Sort Alphabetical

    =?Utf-8?B?YmVub2l0?=, Nov 3, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    11,299
    =?Utf-8?B?U3JlZWppdGggUmFt?=
    Nov 3, 2005
  5. Brano
    Replies:
    3
    Views:
    777
    Kevin Spencer
    Nov 8, 2005
Loading...

Share This Page