views:

61

answers:

2

I have

public class ListA
{

   private string code;
   private string name;

   public string Code { get { return code; } set { code = value; } }
   public string Name { get { return name; } set { name = value; } }

}

List<ListA> lst = new List<ListA>();

    public List<ListA> getList()
    {
        lst.Add(new ListA { Code = "ABC", Name = "Smith" });
        lst.Add(new ListA { Code = "XYZ", Name = "Abbey" });

        return lst;
    }

and in the dropdownlist I want ABC:Smith populated.

ddlList.DataTextField = "Name"; 

only populates the name.

I want to populate both name and code. How do I do that. Please help.

+1  A: 

Add a NameAndCode string property to the ListA class that returns Code + ": " + Name. Then Make that your DataTextField value.

Coov
+3  A: 

It seems that the DataTextField can only be used to display a single property. You can either create a new property on ListA like the following:

public string Display { get { return Code + ":" + Name; } }

or remove the DataTextField altogether and use

public override string ToString() { return Code + ":" + Name; }

or create a new class which inherits from ListA and does one of the things I mentioned above for proper display.

Yuriy Faktorovich
below the dropdownlist I have an asp:label in which I want to populate only the selected Name from the dropdownlist and not code.How do I do that? I can capture the selected value but here I want just the name and not code. Thanks
With either of my solutions you can still set the DataValueField to Name.
Yuriy Faktorovich