简体   繁体   English

如何将ASP.NET下拉列表DataTextField属性绑定到嵌套属性

[英]How to bind the ASP.NET drop down list DataTextField property to a nested property

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. 我想将ASP.NET下拉控件的DataTextField属性绑定到作为初始数据源属性的对象的属性。 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 ASP.NET代码背后

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

MyDropDownList.DataSource = MyList;
MyDropDownList.DataValueField = "ID";

Say you have a List of A, and want A.ID to be the ID field, and ABName 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: 假设你有一个A列表,并希望A.ID是ID字段,而ABName是Name字段,你不能直接绑定到B.Name,所以你要么必须在A上创建一个新属性来拉取从A的B属性中命名,或者您可以使用Linq创建一个匿名类型,为您执行此操作:

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 };
    cmb_category.DataSource = cc.getCat(); //source for database
    cmb_category.DataTextField = "category_name";
    cmb_category.DataValueField = "category_name";
    cmb_category.DataBind();

Here's 2 examples for binding a dropdown in ASP.net from a class 这是从类中绑定ASP.net下拉列表的2个示例

Your aspx page 你的aspx页面

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

Your aspx.cs page 你的aspx.cs页面

    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) 你的jour.cs类(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;
    }
}

您错过了所有重要的DataBind系列!

MyDropDownList.DataBind();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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