简体   繁体   中英

Populating a DropDownlist From a Strongly Typed List

I"m populating a DropDownList from a strongly typed list, and that is working fine. The issue is I want to concatenate two of the fields within the list first and then put them in the dropdown. ie, FirstName + LastName. I've tried a few things that didn't pan out, so can someone give this novice a lil help.

This is an example of what I'm doing.

private List<Customer> _CustomerList = new List<Customer>();

ddlCustomer.DataSource = _CustomerList;
ddlCustomer.DataTextField = "FirstName";
ddlCustomer.DataValueField = "CustomerKey";
ddlCustomer.DataBind();

this works but I need first and last together and I can't manipulate that data in the Customer object.

Try using an enumeration of an anonymous object created on the fly.

var _CustomerList = customers.Select( c => new {
                                         Name = c.FirstName + " " + c.LastName,
                                         Key = c.CustomerKey
                                    });


ddlCustomer.DataSource = _CustomerList;
ddlCustomer.DataTextField = "Name";
ddlCustomer.DataValueField = "Key";
ddlCustomer.DataBind();

You may have to add a ToList() after the Select, but I think you can bind to an IEnumerable<T> .

PS This example requires the .Net 3.5 Framework

您可以在Customer类中创建一个将FirstName和LastName连接在一起的属性,并且可以在ddlCustomer.DataTextField中使用该属性。

I have yet to find a good solution to this problem. I've found the following approach to work the best:

  • Create a wrapper class around Customer (either inherit from Customer if possible, or create an entirely new class that holds a Customer object and exposes further properties.

  • Create a property in your NEW class that concatenates the two fields you wish to databind to.

  • Bind the dropdown to a List of your custom object.

To separate the UI representation from the underlying data object, you can create a wrapper object around Customer, such as CustomerForUI, with a single property called FullName - and then put a list of CustomerForUI objects into the UI.

Something like:

public class CustomerForUI
{
    private string _FullName;

    public CustomerForUI(Customer c)
    {
       _FullName = c.FirstName + " " + c.LastName;
    }

    public string FullName
    {
       get {return _FullName;}
    }

    public string CustomerKey
    {  ... }
}

and construct a list of CustomerForUI objects called _UIList, and:

ddlCustomer.DataSource = __UIList;
ddlCustomer.DataTextField = "FullName";
ddlCustomer.DataValueField = "CustomerKey";
ddlCustomer.DataBind();

Is there a reason why you can't manually build up the list items in the drop-down list? Since you're already doing this in code, I don't see why not.

Just enumerate your business object, and use:

ddlCustomer.Items.Add(new ListItem("text" , "value"));

where text is the first and last name concatenated together.

我同意ScottE ...这似乎是最简单的方法,并且要减少一个对象。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM