Pregunta

Quiero implementar una apariencia como este artículo mencionado usando ListView anidado controlar. Sin embargo, en mi escenario, no puedo usar el control EntityDataSource, por lo que vinculo los datos manualmente.

Mi tabla:

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

Quiero que la subcategoría tenga una sangría agregando una etiqueta < td > al elemento que '' ParentId '' No es nulo. ¿Y mi pregunta es cómo editar las etiquetas html generadas en el evento ItemDataBound?

¿Fue útil?

Solución

Podrías tener algo como esto:

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

en el código subyacente:

protected string GetParentContent(object ParentID)
{
    if(ParentID!=null)
        ... return parent HTML ...
    else
        return "";
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top