【问题标题】:How to bind the ASP.NET drop down list DataTextField property to a nested property如何将 ASP.NET 下拉列表 DataTextField 属性绑定到嵌套属性
【发布时间】:2011-04-19 18:51:44
【问题描述】:

我想将 ASP.NET 下拉控件的 DataTextField 属性绑定到作为初始数据源属性的对象的属性。我将如何完成该特定任务。

下拉数据源数据架构

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 背后的代码

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

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

【问题讨论】:

  • 如果列表中有超过1个B,应该使用哪个B来获取Name属性?
  • @300 波特 - 我已经用正确的场景更新了问题。
  • @Michael - 在您的场景中,您直接绑定 B 列表,而 A(包含您要绑定的 ID)无处可见。
  • @300 Baud - 我修复了源代码中的错误
  • @Michael - 例如,如果GetList 返回一个包含 5 个 A 的列表,而这 5 个 A 中的每一个都包含一个包含 5 个 B 的列表,那么您是否希望 DropDownList 有 5 个项目(1 个每个 A)或 25 个项目(每个 A 中的每个 B 1 个)?

标签: asp.net binding drop-down-menu datatextfield


【解决方案1】:

假设您有一个 A 的列表,并且希望 A.ID 是 ID 字段,并且 A.B.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 };

【讨论】:

    【解决方案2】:
        cmb_category.DataSource = cc.getCat(); //source for database
        cmb_category.DataTextField = "category_name";
        cmb_category.DataValueField = "category_name";
        cmb_category.DataBind();
    

    【讨论】:

      【解决方案3】:

      这里有 2 个在 ASP.net 中从类绑定下拉列表的示例

      你的 aspx 页面

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

      您的 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();     
          }
      

      你的 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;
          }
      }
      

      【讨论】:

      • 该示例实际上并未解决 OP 所询问的问题。
      【解决方案4】:

      您缺少所有重要的 DataBind 行!

      MyDropDownList.DataBind();
      

      【讨论】:

        猜你喜欢
        • 2010-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-01
        • 1970-01-01
        • 2012-07-19
        • 2012-05-02
        • 1970-01-01
        相关资源
        最近更新 更多