5

I want to bind the DataTextField property of a ASP.NET drop down control to a property of an object that is a property of the initial data source. How would I accomplish that particular task.

Drop down data source data schema

public class A
{
   public string ID { get; set; }
   public B { get; set; }
} 

public class B
{
   public string Name { get; set; }  //want to bind the DataTextField to this property
}

ASP.NET code behind

DropDownList MyDropDownList = new DropDownList();
List<A> MyList = GetList();

MyDropDownList.DataSource = MyList;
MyDropDownList.DataValueField = "ID";
Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231
  • If you have more than 1 B in the list, which B should be used to get the Name property? – 300 baud Apr 19 '11 at 19:05
  • @300 baud - I have updated the question with the correct scenario. – Michael Kniskern Apr 19 '11 at 19:11
  • @Michael - In your scenario, you are binding directly against a list of B, and A (which contains the ID you want to bind) is nowhere to be seen. – 300 baud Apr 19 '11 at 19:18
  • @300 Baud - I fixed the error in the source code – Michael Kniskern Apr 19 '11 at 19:51
  • @Michael - So for example, if `GetList` returns a list of 5 A's and each of these 5 A's contains a list of 5 B's, are you expecting your DropDownList to have 5 items (1 for each A) or 25 items (1 for each B in each A)? – 300 baud Apr 19 '11 at 19:57
  • @Michael - It looks like you removed the List to just B (didn't notice that on the last edit). In that case, you probably want to look at Chris Mullins answer. – 300 baud Apr 19 '11 at 20:14

4 Answers4

12

Say you have a List of A, and want A.ID to be the ID field, and A.B.Name to be the Name field, you cannot bind to B.Name directly, so you either have to create a new property on A to pull the name out of the B property of A or you can use Linq to create an anonymous type that does it for you like this:

List<A> ListA = new List<A>{
    new A{ID="1",Item = new B{Name="Val1"}},
    new A{ID="2", Item =  new B{Name="Val2"}} ,          
    new A{ID="3", Item =  new B{Name="Val3"}}};

DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "ID";
DropDownList1.DataSource = from a in ListA
                           select new { ID, Name = a.Item.Name };
Chris Mullins
  • 6,677
  • 2
  • 31
  • 40
0
    cmb_category.DataSource = cc.getCat(); //source for database
    cmb_category.DataTextField = "category_name";
    cmb_category.DataValueField = "category_name";
    cmb_category.DataBind();
Kisan
  • 1
0

Here's 2 examples for binding a dropdown in ASP.net from a class

Your aspx page

    <asp:DropDownList ID="DropDownListJour1" runat="server">
    </asp:DropDownList>
    <br />
    <asp:DropDownList ID="DropDownListJour2" runat="server">
    </asp:DropDownList>

Your aspx.cs page

    protected void Page_Load(object sender, EventArgs e)
    {
    //Exemple with value different same as text (dropdown)
    DropDownListJour1.DataSource = jour.ListSameValueText();            
    DropDownListJour1.DataBind();

    //Exemple with value different of text (dropdown)
    DropDownListJour2.DataSource = jour.ListDifferentValueText();
    DropDownListJour2.DataValueField = "Key";
    DropDownListJour2.DataTextField = "Value";
    DropDownListJour2.DataBind();     
    }

Your jour.cs class (jour.cs)

public class jour
{

    public static string[] ListSameValueText()
    {
        string[] myarray = {"a","b","c","d","e"} ;
        return myarray;
    }

    public static Dictionary<int, string> ListDifferentValueText()
    {
        var joursem2 = new Dictionary<int, string>();
        joursem2.Add(1, "Lundi");
        joursem2.Add(2, "Mardi");
        joursem2.Add(3, "Mercredi");
        joursem2.Add(4, "Jeudi");
        joursem2.Add(5, "Vendredi");
        return joursem2;
    }
}
Pascal Carmoni
  • 554
  • 4
  • 8
0

You are missing the all important DataBind line!

MyDropDownList.DataBind();
csharpsql
  • 2,190
  • 1
  • 19
  • 24