【发布时间】:2015-08-13 13:20:50
【问题描述】:
我有一个使用自定义数据源的 ListView 控件。它将很快成为一个数据库,但目前我仍在努力让它运行。
在我的表单的 HTML 中,我有以下代码:
<%-- Ref: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.layouttemplate.aspx --%>
<asp:ListView ID="lvOfficers" runat="server"
OnSelectedIndexChanging="lvOfficers_SelectedIndexChanging"
OnSelectedIndexChanged="lvOfficers_SelectedIndexChanged">
<LayoutTemplate>
<table id="tblOfficers">
<tr>
<th style="width:20%"></th>
<th colspan="2">Member Name</th>
<th style="width:20%"></th>
<th style="width:20%"></th>
</tr>
<tr>
<th>Title</th>
<th>Last</th>
<th>First</th>
<th>Phone</th>
<th>Email</th>
</tr>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><asp:Label ID="lblRank" runat="server" Text='<#Eval("Rank") %>'></asp:Label></td>
<td><asp:Label ID="lblLast" runat="server" Text='<#Eval("LastName") %>'></asp:Label></td>
<td><asp:Label ID="lblFirst" runat="server" Text='<#Eval("FirstName") %>'></asp:Label></td>
<td><asp:Label ID="lblPhone" runat="server" Text='<#Eval("HomePhone") %>'></asp:Label></td>
<td><asp:Label ID="lblEmail1" runat="server" Text='<#Eval("PersonalEmail") %>'></asp:Label></td>
</tr>
</ItemTemplate>
</asp:ListView>
在 Page_Load 后面的代码中,我填充了数据:
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
GetOfficers();
}
}
protected void GetOfficers() {
lvOfficers.DataSource = Personnel.GetOfficers();
lvOfficers.DataBind();
}
我可以在调试模式下单步执行,并看到在我的“GetOfficers”数据工厂生成的 ListView 中有 8 个类型为 Person 的元素。
Person 类包含子类:
电话类:对于 {
Home,Cell,Work},Address 类 {
Primary,Secondary} 包含用于指示联系方式的排名逻辑,以及会员类。一个跟踪成员的
ID、JoinDate和Expiration,而第二个实例跟踪LifeMembership 的相同元素。
考虑到这个复杂的 Person 类导致了我的问题,我编辑了 GetOfficers() 方法以使用匿名列表:
protected void GetOfficers() {
var officers = Personnel.GetOfficers();
var data = from o in officers
select new {
o.Rank,
o.LastName,
o.FirstName,
o.HomePhone,
o.PersonalEmail,
};
//var data = (from o in officers
// select new {
// o.Rank,
// o.LastName,
// o.FirstName,
// o.HomePhone,
// o.PersonalEmail,
// }).OrderBy(o => o.Rank).OrderBy(o => o.LastName);
lvOfficers.DataSource = data;
lvOfficers.DataBind();
}
一开始我很确定这会解决我的问题,所以我加入了两个 OrderBy 子句。当它不起作用时,我将其注释掉再试一次,但仍然没有成功。
无论我尝试什么,页面都会显示“Eval”文本而不是实际数据。
我猜答案很简单,但我忽略了,因为我使用相同的技术在同一个项目的其他页面中填充数据。
如何显示我的数据?
【问题讨论】:
-
Text='<#Eval("Rank") %>'应该是Text='<%#Eval("Rank") %>'
标签: c# asp.net listview data-binding