Databind ASP.NET List of ListItem to DropDownList issue

78,756

Solution 1

Hi when databinding (to anything) you need to set the DataTextField and DataValueField of your DropDownList. In your case you should use the following code

List<ListItem> users = new List<ListItem>();
foreach (SubscriptionUser su in subscriptionDetails.UserList)
{
    users.Add(new ListItem(su.FirstName + " " + su.LastName, su.EmailAddress));
}
ddlPrimaryContact.DataTextField = "Text";
ddlPrimaryContact.DataValueField = "Value";
ddlPrimaryContact.DataSource = users;
ddlPrimaryContact.DataBind();

Solution 2

You should bind your dropdown list as :

ddlPrimaryContact.DataSource = users;
ddlPrimaryContact.DataTextField = "Value";
ddlPrimaryContact.DataValueField = "Text";
ddlPrimaryContact.DataBind();

If you ask why, as far as I know, databound controls takes texts and values (if they are not supplied like above) by calling the ToString method for each item in datasource collection. So each ListItem in your collection return it's Text property by ToString method.

Solution 3

Or, alternatively, you could bind it this way. (Assuming you can add a readOnly property to SubscriptionUser called FullName (which returns su.FirstName + " " + su.LastName)

ddlPrimaryContact.DataSource = subscriptionDetails.UserList;
ddlPrimaryContact.DataBind();

then, in your ASPX page put:

<asp:DropDownList id="ddlPrimaryContact" runat="server" DataTextField="FullName" DataValueField="EmailAddress" />

Solution 4

you can aslo directly add listitem to dropdown..

List<ListItem> users = new List<ListItem>();
        for (int count = 0; count < 10; count++)
        {
            ListItem li = new ListItem("List " + count.ToString(), count.ToString());
            ddl.Items.Add(li);
        }

Solution 5

There is a way to add ListItem items to a DropDownList and having .NET automagically match the ListItem's Text with the DropDownList's Text, and the same for Value:

List<ListItem> users = new List<ListItem>();
// fill in list
ddlUsers.Items.AddRange(users.ToArray());
Share:
78,756
Farinha
Author by

Farinha

...

Updated on July 25, 2020

Comments

  • Farinha
    Farinha almost 4 years

    I've just come across this bizarre thing that I was expecting to work in a different (logical) way, but it doesn't. Is it a bug or a "feature"?

    So there's a DropDownList that I'm populating in the codebehind with a List of ListItem. Each new ListItem gets 2 arguments that, according to the intellisense-provided documentation, correspond to its text and value:

    List<ListItem> users = new List<ListItem>();
    foreach (SubscriptionUser su in subscriptionDetails.UserList)
    {
        users.Add(new ListItem(su.FirstName + " " + su.LastName, su.EmailAddress));
    }
    ddlPrimaryContact.DataSource = users;
    ddlPrimaryContact.DataBind();
    

    Now, can someone explain me why the databound DropDownList both the Text and Value set to exactly the same (the ListItem text) instead of using ListItem.Text as the Text and ListItem.Value as the Value?

    ARGH!! http://www.freeimagehosting.net/uploads/fe65d0e7d5.jpg

    Or am I doing something wrong?