Come aggiungere a < td > tag a ListViewItem?
-
03-07-2019 - |
Domanda
Voglio implementare un aspetto come questo articolo menzionato usando ListView nidificato controllo. Tuttavia, nel mio scenario, non riesco a utilizzare il controllo EntityDataSource, quindi associo i dati manualmente.
La mia tabella:
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();
}
}
Voglio che la sottocategoria abbia un rientro aggiungendo un tag < td >
all'elemento che " ParentId " non è nullo. E la mia domanda è: come modificare i tag HTML generati nell'evento ItemDataBound?
Soluzione
Potresti avere qualcosa del genere:
<ItemTemplate>
<tr>
<td colspan="2"><%# GetParentContent(Eval("ParentID")) %></td>
</tr>
</ItemTemplate>
nel code-behind:
protected string GetParentContent(object ParentID)
{
if(ParentID!=null)
... return parent HTML ...
else
return "";
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow