views:

228

answers:

2

What i'm I missing?

My line "foreach (ProductCrash productCrash in _fiveLastest)" is wrong, but I can't see any alternatives. I wont to get ride out the foreach and let the ListView do its magic for me.. does any know how to do this? It would really help me out.

I can only show one object from my list in my ListView. I'm using ObjectDataSource to bind my list with the ListView, and don't want to change that.

In my ascx.cs fil i retrieve the List _fiveLastest, with Entity.

    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); 
                }
            }
        }
    }

=========

Here's a codesnippet of my ListView, ItemTemplate, LayoutTemplate and 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>
+2  A: 

The ItemDataBound event is fired once for every item that is bound, meaning that if your ObjectDataSource1 has 10 items, the event will fire 10 times.

What you do, is setting the TimeStampLabel to all the values of the fiveLastest, and the last value of the loop sticks.

Basically, get rid of the foreach loop, and take a look of the e.Item.DataItem property. That is the DataItem the row of the list view is bound to.

SWeko
I dont think my ObjectDataSource1 has any items bound to it? I only get the ItemDataBound event to be fired once.I guess what im trying to hack is that, to set the ListView in one go
radbyx
then get rid of the ObjectDataSource1 altogether, and bind the grid to the _fiveLastest list. The code posted by Spyros will work then.
SWeko
+1 vote for telling me to get rid of the ObjectDataSource1, thx alot.
radbyx
+2  A: 

Set the objectDatasource of the listview item in page load

protected void Page_Load(object sender, EventArgs e)
{
    _fiveLastest = _dataAccess.TimeStampForCrashByIndex();
    _fiveLastest.Sort((x, y) => y.CrashTimeStamp.CompareTo(x.CrashTimeStamp));
    listViewControl.DataSource = _fiveLastest;
    listViewControl.DataBind();
}

The databound event will fire for every object in the datasource so to display information for each object in the databound event write this

protected void ListView2_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        Label statusLabel = (Label)e.Item.FindControl("TimeStampLabel");
        Label productIDLabel = (Label)e.Item.FindControl("ProductIDLabel");
        //get the current ProductCrash object
        var productCrash = (ProductCrash)e.Item.DataItem;
        productIDLabel.Text = productCrash.ProductName;
        statusLabel.Text = productCrash.CrashTimeStamp; 
     }
}
Spyros
Thx. I just bind the DataSource with my ListView.I can't get the "DataItem" in e.Item.DataItem though.My bad workaround is to use an index ProductCrash productCrash = _fiveLastest[index];, but ofcourse it's not optimal.
radbyx
When u say you can't get the dataitem from e.Item.DataItem you mean that you have an exception or you take a null object?
Spyros
I wrote the same code on my pc and it worked perfectly without using ObjectDataSource and DataSourceID in ListView control whats the error that you get?
Spyros
my codeassist can find `e.Item` but not `e.Item.DataItem`
radbyx
Are you sure that the e object you have is type of ListViewItemEventArgs ?
Spyros
Yes. protected void ListView2_ItemDataBound(object sender, ListViewItemEventArgs e) { if (e.Item.ItemType == ListViewItemType.DataItem) { Label statusLabel = (Label)e.Item.FindControl("TimeStampLabel"); Label productIDLabel = (Label)e.Item.FindControl("ProductIDLabel"); if (index < _fiveLastest.Count) {// e.Item.DataItem; Can't reconice "DataItem" here ProductCrash productCrash = _fiveLastest[index];Strange :/
radbyx
yes it's the same.
radbyx

related questions