2009-05-03 19 views
0

我想使用嵌套的ListView控件提到this article的外觀。但是,在我的情況下,我無法使用EntityDataSource控件,因此我手動綁定了數據。如何將一個​​標記添加到ListViewItem?

我的表:

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

我想要的子類別有縮進由<td>標籤添加到「的ParentId」不爲空的項目。而我的問題是如何編輯ItemDataBound事件中生成的html標籤?

回答

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 ""; 
} 
+0

THX了很多,它的作品! – 2009-05-04 15:59:39

相關問題