C# 由属性组成的下拉列表 DataTextField?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/409648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:15:34  来源:igfitidea点击:

dropdownlist DataTextField composed from properties?

c#asp.netdata-bindingdrop-down-menu

提问by karlis

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 e.g. not use "Name", but "Name (Zip)" eg.

我想要例如不使用“名称”,而是“名称(邮编)”例如。

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 中执行我需要的操作)。

采纳答案by M4N

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). This can also be done using some LINQ:

或者如果不能修改MyObject,则在表现层创建一个包装对象(仅用于显示)。这也可以使用一些 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,所以代码中可能有错误)

回答by ecoffey

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. So you would map your Model (My Object in your example) to a ViewModel object, and then bind the drop down list that way. It's a cool way to think about separation of concerns.

本质上,您希望创建一个表示绑定到特定 UI 的类。因此,您可以将您的模型(在您的示例中为我的对象)映射到一个 ViewModel 对象,然后以这种方式绑定下拉列表。这是考虑关注点分离的一种很酷的方式。

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

回答by Achilles

BTW, Try assigning the "DataTextField" and "DataValueField" before you assign the DataSource. Doing so will prevent firing the "SelectedIndexChanged" event while databinding...

顺便说一句,在分配数据源之前尝试分配“DataTextField”和“DataValueField”。这样做将防止在数据绑定时触发“SelectedIndexChanged”事件...