【问题标题】:How to add a <td> tag to ListViewItem?如何将 <td> 标签添加到 ListViewItem?
【发布时间】:2009-05-03 14:00:33
【问题描述】:

我想使用嵌套的 ListView 控件实现 this article 中提到的外观。但是,在我的场景中,我不能使用 EntityDataSource 控件,所以我手动绑定数据。

我的桌子:

Categories
  PK: UniqueId, Guid
  Name, string
  ParentId, Guid

<asp:ListView ID="CategoryList" runat="server" 
        onitemdatabound="CategoryList_ItemDataBound">
        <LayoutTemplate>
            <table>
                <asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>
            </table>
        </LayoutTemplate>

        <ItemTemplate>
            <tr>
                <td colspan="2"><%# Eval("Name") %></td>
            </tr>
        </ItemTemplate>
    </asp:ListView>



protected void Page_Load(object sender, EventArgs e)
{
    using (PractiseEntities context = new PractiseEntities()) {
        var result = from categories in context.Categories
                     select categories;
        CategoryList.DataSource = result;
        CategoryList.DataBind();
    }
}

我希望通过在“ParentId”不为空的项目中添加&lt;td&gt; 标记来使子类别缩进。而我的问题是如何在ItemDataBound事件中编辑生成的html标签?

【问题讨论】:

    标签: asp.net data-binding listview


    【解决方案1】:

    你可以有这样的东西:

    <ItemTemplate>
        <tr>
            <td colspan="2"><%# GetParentContent(Eval("ParentID")) %></td>
        </tr>
    </ItemTemplate>
    

    在代码隐藏中:

    protected string GetParentContent(object ParentID)
    {
        if(ParentID!=null)
            ... return parent HTML ...
        else
            return "";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      • 2016-08-04
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多