我有一个代码块,返回一个员工对象列表。
结果集包含多个员工记录。其中一个元素是EmployeeID。
我需要用只有EmployeeID的方式填充listview(lstDepartment)。我该怎么做?
lstDepartment.DataSource = oCorp.GetEmployeeList(emp);
lstDepartment.DataBind()
您还需要指定以下内容:
lstDepartment.DataSource = oCorp.GetEmployeeList(emp);
lstDepartment.DataTextField = "EmployeeID";
lstDepartment.DataValueField = "EmployeeID";
lstDepartment.DataBind()
lstDepartment.DataSource = oCorp.GetEmployeeList(emp)
.Select(emp => new { emp.EmployeeID });
lstDepartment.DataBind();
编辑:但你也可以选择所有列,只显示其中一列。ListView
不是ListBox
或DropDownList
。只有你使用的内容才会被显示。所以如果你的ItemTemplate看起来像:
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label ID="LblEmployeeID" runat="server" Text='<%# Eval("EmployeeID") %>' />
</td>
</tr>
无论您的DataSource
中有什么内容,只有EmployeeID
会被显示。
您可以在 ListView 的 ItemTemplate
中指定要显示的内容。
<asp:ListView ID="lstDepartment" runat="server">
<ItemTemplate>
<p> <%#Eval("EmployeeID") %> </p>
</ItemTemplate>
</asp:ListView>