如何使用 ObjectDataSource 用列表填充 ListView

发布于 2024-09-04 05:52:03 字数 3848 浏览 4 评论 0原文

我缺少什么?

我的行“foreach (ProductCrash ProductCrash in _ FiveLastest)”是错误的,但我看不到任何替代方案。我不想摆脱 foreach 并让 ListView 为我发挥它的魔力..有人知道如何做到这一点吗?这真的会帮助我。

我只能在 ListView 中显示列表中的一个对象。我正在使用 ObjectDataSource 将列表与 ListView 绑定,并且不想更改它。

在我的 ascx.cs 文件中,我使用实体检索列表_ FiveLastest

    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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅气称霸 2024-09-11 05:52:03

在页面加载中设置列表视图项的 objectDatasource

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

数据源中的每个对象都会触发 databound 事件
因此,要在数据绑定事件中显​​示每个对象的信息,请编写以下内容

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

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; 
     }
}
单挑你×的.吻 2024-09-11 05:52:03

对于每个绑定的项目,ItemDataBound 事件都会触发一次,这意味着如果您的 ObjectDataSource1 有 10 个项目,则该事件将触发 10 次。

您要做的就是将 TimeStampLabel 设置为 FiveLastest 的所有值,并保留循环的最后一个值。

基本上,摆脱 foreach 循环,并查看 e.Item.DataItem 属性。这是列表视图的行绑定到的 DataItem。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文