我错过了什么?
“我的台词”
foreach (ProductCrash productCrash in _fiveLastest)
“这是错误的,但我看不到任何替代品。我习惯于骑出foreach让ListView
帮我变魔术吧
.. 有人知道怎么做吗?这对我真的很有帮助。
我只能表现出来
一
对象从我的列表视图中的列表中删除。我正在使用ObjectDataSource将我的列表与ListView绑定,不想更改它。
在ascx.cs文件中,我检索列表
_五级
,具有实体。
private List<ProductCrash> _fiveLastest;
protected void Page_Load(object sender, EventArgs e)
{
index = 0;
_dataAccess = new DataAccess();
_fiveLastest = _dataAccess.TimeStampForCrashByIndex();
_fiveLastest.Sort((x, y) => y.CrashTimeStamp.CompareTo(x.CrashTimeStamp));
}
protected void ListView2_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
foreach (ProductCrash productCrash in _fiveLastest)
{
Label statusLabel = (Label)e.Item.FindControl("TimeStampLabel");
Label productIDLabel = (Label)e.Item.FindControl("ProductIDLabel");
if (index < _fiveLastest.Count)
{
productIDLabel.Text = productCrash.ProductName;
statusLabel.Text = DatetimeHelper.MyDateTimeFormat(productCrash.CrashTimeStamp);
}
}
}
}
下面是我的ListView、ItemTemplate、LayoutTemplate和ObjectDataSource的代码片段
<asp:ListView ID="ListView2" runat="server" DataSourceID="ObjectDataSource1" onitemdatabound="ListView2_ItemDataBound">
<ItemTemplate>
<tr style="background-color: #FFFFFF;color: #000000;">
<td style="padding: 5px 5px;">
<asp:Label ID="ProductIDLabel" runat="server" />
</td>
<td style="padding: 5px 5px;">
<asp:Label ID="TimeStampLabel" runat="server" />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="itemPlaceholderContainer" runat="server" border="1"
style=" background-color: #FFFFFF;border-color: #999999;border-style:solid;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
<tr id="Tr1" runat="server" style="background-color: #DCDCDC;color: #333333; text-align:left; padding: 5px 10px;">
<th runat="server" style="padding: 5px 5px; text-transform:uppercase;">
Produkt</th>
<th runat="server" style="padding: 5px 5px; text-transform:uppercase;">
Tidspunkt</th>
</tr>
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
</table>
</LayoutTemplate>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="CreateStateLog" TypeName="Website.StateLog"
UpdateMethod="CreateStateLog">
<UpdateParameters>
<asp:Parameter Name="stateLogID" Type="Int32" />
<asp:Parameter Name="productID" Type="Int32" />
<asp:Parameter Name="status" Type="Int32" />
<asp:Parameter Name="timeStamp" Type="DateTime" />
</UpdateParameters>
<SelectParameters>
<asp:Parameter Name="stateLogID" Type="Int32" />
<asp:Parameter Name="productID" Type="Int32" />
<asp:Parameter Name="status" Type="Int32" />
<asp:Parameter Name="timeStamp" Type="DateTime" />
</SelectParameters>
</asp:ObjectDataSource>