【问题标题】:Display property of object in listview在列表视图中显示对象的属性
【发布时间】:2015-12-14 20:47:11
【问题描述】:

所以我必须在 asp.net 网络表单的列表视图中显示产品类型列表

到目前为止,我已经能够填充列表视图并使用 container.DataItem 显示对象的类型,但我找不到如何显示产品类型的名称。

后面的代码是这样的:

protected void Page_Load(object sender, EventArgs e)
        {
            IList<ProductType> productTypes = getProductTypes();
            ListView1.DataSource = productTypes;
            ListView1.DataBind();
        }

还有.aspx:

<asp:ListView ID="ListView1" runat="server">
    <LayoutTemplate>
        <table width="200px">
                <tr runat="server" id="itemPlaceholder">
                </tr>
            </table>
    </LayoutTemplate>
    <ItemTemplate>
        <asp:Label Text="<% WHRE THE NAME SHOULD BE DISPLAYED %>" runat="server" />
    </ItemTemplate>
</asp:ListView>

到目前为止,我已经看到在某些情况下,他们使用“Eval()”和属性名称,但在这些情况下,对象是在代码中定义的,这对我来说不是一个选项。

【问题讨论】:

    标签: c# asp.net listview webforms


    【解决方案1】:

    你能用foreach吗? ListView 相当死板。通过这种方式,您对您的代码拥有更大的权力,而实际上没有更多工作要做:

    代码隐藏:

    public List<ProductType> productTypes;
    protected void Page_Load(object sender, EventArgs e)
    {
        productTypes = getProductTypes();
    }
    

    及页面代码:

    <table>
        <% foreach(var p in productTypes) {%>
            <tr>
                <td><% = p.Id %></td><td><% = p.Name %></td>
            </tr>
        <% } %>
    </table>
    

    【讨论】:

    • 不要忘记使用公共修饰符(代码中的第一行)公开模型,否则页面前端将无法访问。 MVC 就是这样工作的,你应该进入它。
    猜你喜欢
    • 2018-06-18
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 2014-04-16
    • 1970-01-01
    相关资源
    最近更新 更多