C# ASP.NET:列表框数据源和数据绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13879275/
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-10 10:02:15  来源:igfitidea点击:

ASP.NET: Listbox datasource and databind

c#asp.netlistbox

提问by user1889838

I have an empty listbox on .aspx page

我在 .aspx 页面上有一个空列表框

lstbx_confiredLevel1List

I am generating two lists programatically

我正在以编程方式生成两个列表

List<String> l1ListText = new List<string>(); //holds the text 
List<String> l1ListValue = new List<string>();//holds the value linked to the text

I want to load lstbx_confiredLevel1Listlist box on .aspx page with above values and text. So i am doing following:

我想lstbx_confiredLevel1List在 .aspx 页面上加载具有上述值和文本的列表框。所以我正在做以下事情:

lstbx_confiredLevel1List.DataSource = l1ListText;
lstbx_confiredLevel1List.DataTextField = l1ListText.ToString();
lstbx_confiredLevel1List.DataValueField = l1ListValue.ToString();
lstbx_confiredLevel1List.DataBind();

but it does not load the lstbx_confiredLevel1Listwith l1ListTextand l1ListValue.

但它不会加载lstbx_confiredLevel1Listwithl1ListTextl1ListValue

Any ideas?

有任何想法吗?

采纳答案by Tim Schmelter

Why don't you use the same collection as DataSource? It just needs to have two properties for the key and the value. You could f.e. use a Dictionary<string, string>:

为什么不使用与 相同的集合DataSource?它只需要具有键和值的两个属性。你可以使用一个Dictionary<string, string>

var entries = new Dictionary<string, string>();
// fill it here
lstbx_confiredLevel1List.DataSource = entries;
lstbx_confiredLevel1List.DataTextField = "Value";
lstbx_confiredLevel1List.DataValueField = "Key";
lstbx_confiredLevel1List.DataBind();

You can also use an anonymous type or a custom class.

您还可以使用匿名类型或自定义类。

Assuming that you have already these lists and you need to use them as DataSource. You could create a Dictionaryon the fly:

假设您已经有了这些列表,并且需要将它们用作数据源。您可以动态创建一个Dictionary

Dictionary<string, string> dataSource = l1ListText
           .Zip(l1ListValue, (lText, lValue) => new { lText, lValue })
           .ToDictionary(x => x.lValue, x => x.lText);
lstbx_confiredLevel1List.DataSource = dataSource;

回答by GeorgesD

You'd better used a dictionnary:

你最好使用字典:

Dictionary<string, string> list = new Dictionary<string, string>();
...
lstbx_confiredLevel1List.DataSource = list;
lstbx_confiredLevel1List.DataTextField = "Value";
lstbx_confiredLevel1List.DataValueField = "Key";
lstbx_confiredLevel1List.DataBind();

回答by Destrictor

Unfortunately the DataTextFieldand DataValueFieldare not used like that. They are the text representation of the fields they're supposed to show of the current item that's being databound in the DataSource.

不幸的是DataTextFieldDataValueField不是这样使用的。它们是它们应该显示的字段的文本表示,这些字段是在 DataSource 中进行数据绑定的当前项目。

If you had an object that held both text and value, you'd make a list of it and set that to datasource like this:

如果您有一个同时包含文本和值的对象,您可以列出它并将其设置为数据源,如下所示:

public class MyObject {
  public string text;
  public string value;

  public MyObject(string text, string value) {
    this.text = text;
    this.value = value;
  }
}

public class MyClass {
  List<MyObject> objects;
  public void OnLoad(object sender, EventArgs e) {
    objects = new List<MyObjcet>();
    //add objects
    lstbx.DataSource = objects;
    lstbx.DataTextField = "text";
    lstbx.DataValueField = "value";
    lstbx.DataBind();
  }
}