本文介绍了绑定多个字段在ASP.NET列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的asp.net,尤其是LINQ和SQL。

I'm fairly new to asp.net and especially LINQ and SQL.

说我有一个表员工有田姓氏,名字和ID。

Say I have a table "Employees" with fields "Lastname", "Firstname", and "ID".

我想这个绑定到一个列表框。我想在列表框中显示它的内容,如$ LASTNAME,$名字,我想每个项目的价值是ID。

I want to bind this to a list box. I want the list box to display it's contents like "$LASTNAME, $FIRSTNAME" and I want the value of each item to be "ID".

这是微不足道的绑定或者名字列到列表框中,并相应的值,但我无法弄清楚如何做到这一点..

It's trivial to bind either name column to the listbox and the values accordingly, but I can't figure out how to do this..

在此先感谢

推荐答案

您可以尝试这样的事:

var datasource = from employee in employees
                 select new
                 {
                     Name = employee.lastName + ", " + employee.firstName,
                     Id = employee.ID
                 };

myListBox.DataSource = datasource;
myListBox.DataTextField = "Name";
myListBox.DataValueField = "Id";
myListBox.DataBind();

此构造匿名类型的列表,你的员工表到你的列表框绑定。

This constructs a list of anonymous types from your employee table to bind your listbox to.

这篇关于绑定多个字段在ASP.NET列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:15