简体   繁体   English

dropdownlist DataTextField由属性组成?

[英]dropdownlist DataTextField composed from properties?

is there a way to make the datatextfield property of a dropdownlist in asp.net via c# composed of more than one property of an object? 有没有办法通过由对象的多个属性组成的c#在asp.net中创建下拉列表的datatextfield属性?

public class MyObject
{
  public int Id { get; set; }
  public string Name { get; set; }
  public string FunkyValue { get; set; }
  public int Zip { get; set; }
}

protected void Page_Load(object sender, EventArgs e)
{
  List<MyObject> myList = getObjects();
  ddList.DataSource = myList;
  ddList.DataValueField = "Id";
  ddList.DataTextField = "Name";
  ddList.DataBind();
}

I want eg not use "Name", but "Name (Zip)" eg. 我想要不要使用“名称”,而是“名称(Zip)”,例如。

Sure, i can change the MyObject Class, but i don't want to do this (because the MyObject Class is in a model class and should not do something what i need in the UI). 当然,我可以更改MyObject类,但我不想这样做(因为MyObject类在模型类中,不应该在UI中做我需要的东西)。

Add another property to the MyObject class and bind to that property : 将另一个属性添加到MyObject类并绑定到该属性:

public string DisplayValue
{
 get { return string.Format("{0} ({1})", Name, Zip); }
}

Or if you can not modify MyObject, create a wrapper object in the presentation layer (just for displaying). 或者,如果您无法修改MyObject,请在表示层中创建一个包装器对象(仅用于显示)。 This can also be done using some LINQ: 这也可以使用一些LINQ来完成:

List<MyObject> myList = getObjects();
ddList.DataSource = (from obj in myList
                    select new
                    {
                      Id = obj.Id,
                      Name = string.Format("{0} ({1})", obj.Name, obj.Zip)
                    }).ToList();
ddList.DataValueField = "Id";
ddList.DataTextField = "Name";
ddList.DataBind();

(sorry I don't have Visual Studio available, so there might be errors in the code) (抱歉,我没有Visual Studio,因此代码中可能存在错误)

I would recommend reading this: http://martinfowler.com/eaaDev/PresentationModel.html 我建议阅读这篇: http//martinfowler.com/eaaDev/PresentationModel.html

Essentially you want to create a class that represents binding to a particular UI. 基本上,您希望创建一个表示绑定到特定UI的类。 So you would map your Model (My Object in your example) to a ViewModel object, and then bind the drop down list that way. 因此,您可以将Model(示例中的My Object)映射到ViewModel对象,然后以这种方式绑定下拉列表。 It's a cool way to think about separation of concerns. 考虑分离问题是一种很酷的方式。

EDIT: Here is another blog series on ViewModel: http://blogs.msdn.com/dancre/archive/2006/10/11/datamodel-view-viewmodel-pattern-series.aspx 编辑:这是另一个关于ViewModel的博客系列: http//blogs.msdn.com/dancre/archive/2006/10/11/datamodel-view-viewmodel-pattern-series.aspx

BTW, Try assigning the "DataTextField" and "DataValueField" before you assign the DataSource. 顺便说一下,在分配DataSource之前,尝试分配“DataTextField”和“DataValueField”。 Doing so will prevent firing the "SelectedIndexChanged" event while databinding... 这样做可以防止在数据绑定时触发“SelectedIndexChanged”事件...

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

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