Asp.net 如何添加<;td>;标记到ListViewItem?

Asp.net 如何添加<;td>;标记到ListViewItem?,asp.net,data-binding,listview,Asp.net,Data Binding,Listview,我想使用嵌套的ListView控件实现前面提到的外观。然而,在我的场景中,我不能使用EntityDataSource控件,所以我手动绑定数据 我的桌子: Categories PK: UniqueId, Guid Name, string ParentId, Guid <asp:ListView ID="CategoryList" runat="server" onitemdatabound="CategoryList_ItemDataBound">

我想使用嵌套的ListView控件实现前面提到的外观。然而,在我的场景中,我不能使用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();
    }
}
类别
PK:UniqueId,Guid
名称、字符串
父ID,Guid
受保护的无效页面加载(对象发送方、事件参数e)
{
使用(PracticeEntities上下文=新PracticeEntities()){
var result=来自上下文中的类别。类别
选择类别;
CategoryList.DataSource=结果;
CategoryList.DataBind();
}
}

我希望通过向“ParentId”不为null的项添加
标记,子类别具有缩进。我的问题是如何在ItemDataBound事件中编辑生成的html标记?

您可以有如下内容:

<ItemTemplate>
    <tr>
        <td colspan="2"><%# GetParentContent(Eval("ParentID")) %></td>
    </tr>
</ItemTemplate>
protected string GetParentContent(object ParentID)
{
    if(ParentID!=null)
        ... return parent HTML ...
    else
        return "";
}