1

我有一个使用 EF 构建 DB 和 LINQ 来访问该数据的网站。我正在尝试将数据打印到列表视图中,但没有打印出来,我不知道为什么。

标记:

<asp:ListView ID="lvSearchResults" OnPagePropertiesChanged="lvSearchResults_PagePropertiesChanged" runat="server">
    <LayoutTemplate>
        <table style="width: 100%" border="1">
            <tr style="text-align:center;">
                <td>Item Name</td>
                <td>Release Region</td>
                <td style="width: 100px">Factory Sealed</td>
                <td style="width: 50px">Item Details</td>
                <td style="width: 50px">Edit Item</td>
            </tr>

            <tr id="itemPlaceHolder" runat="server" />
        </table>

        <asp:DataPager ID="dpSearchResults" PagedControlID="lvSearchResults" PageSize="10" runat="server">
            <Fields>
                <asp:NextPreviousPagerField ButtonType="Button" ShowNextPageButton="false" ShowFirstPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="true" />
                <asp:NumericPagerField ButtonCount="10" />
                <asp:NextPreviousPagerField ButtonType="Button" ShowNextPageButton="true" ShowFirstPageButton="false" ShowLastPageButton="true" ShowPreviousPageButton="false" />
            </Fields>
        </asp:DataPager>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <%#:Item.Name%>
            </td>
            <td>
                <%#:Item.Region%>
            </td>
            <td style="text-align: center;">
                <%#:Item.Condition%>
            </td>
            <td>
                <asp:Button ID="btnSelect" Text="View Details" PostBackUrl="/Items/<%#:ItemName%>" runat="server" />
            </td>
            <td>
                <asp:Button ID="btnEdit" Text="Edit Item" PostBackUrl="/Edit/<%#:ItemName%>" runat="server" />
            </td>
        </tr>
    </ItemTemplate>
</asp:ListView>

代码隐藏:

byte SelectedType = byte.Parse(ddlCategories.SelectedValue);

using (var db = new FullContext())
{
    var q = (from i in db.Items
             where i.TypeID == SelectedType || SelectedType == 0 //Get certain type or all types
             orderby i.Name
             select new
             {
                 i.ID,
                 i.Name,
                 i.Region,
                 i.Condition
             }).ToList();
    lvSearchResults.DataSource = q;
    lvSearchResults.DataBind();
}
4

1 回答 1

0

解决方案是添加 Eval() 命令,因此工作代码是

                               <td>
                                <%#:Eval("Name")%>
                            </td>

                            <td>
                                <%#:Eval("Region")%>
                            </td>

                            <td style="text-align: center;">
                                <%#:Eval("Condition")%>
                            </td>
于 2013-01-20T14:49:18.697 回答