< td>を追加する方法ListViewItemへのタグ?
-
03-07-2019 - |
質問
この記事として、ネストされた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();
}
}
&quot; ParentId&quot;のアイテムに&lt; td&gt;
タグを追加して、サブカテゴリにインデントを設定します。 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 "";
}
所属していません StackOverflow