2011-10-16 20 views
1

我想顯示一個網格視圖控件中的導航屬性的字段值。 我的實體是相冊和照片,照片實體必須與相冊實體相關聯。如何在GridView控件中顯示導航屬性?

我想顯示一個相冊的名稱,但它從來沒有顯示在gridview控件中。

以下是我的標記:

<h4> 
    <asp:DropDownList ID="ddlAlbums" runat="server" Style="padding: 3px; width: 150px;" 
     AutoPostBack="True"> 
    </asp:DropDownList> 

</h4> 
<br /> 
<asp:UpdatePanel ID="pnlPhotos" runat="server" > 
    <Triggers> 
     <asp:AsyncPostBackTrigger ControlID="ddlAlbums" /> 
    </Triggers> 
    <ContentTemplate> 
     <asp:DataList ID="PhotosDataList" runat="server" DataKeyField="PhotoID" DataSourceID="PhotosEntityDataSource" 
      RepeatDirection="Horizontal" CssClass="view" RepeatColumns="5" 
      onitemdatabound="PhotosDataList_ItemDataBound"> 
      <ItemTemplate> 
       <asp:Image ID="img" CssClass="thumbnail" ToolTip='<%#Eval("Title")%>' runat="server" 
        ImageUrl='<%#Eval("Caption","~/Uploads/Pictures/{0}")%>' /> 
       <br /> 
      **<asp:Label ID="lblAlbumName" runat="server" Text='<%#Eval("Album.Name")%>'></asp:Label>** 

       <asp:LinkButton ID="lnkEdit" runat="server" CommandName="Select" CommandArgument='<%#Eval("PhotoID")%>' 
        OnClick="lnkEdit_Click">edit</asp:LinkButton>&nbsp; 
       <asp:LinkButton ID="lnkDelete" runat="server" CommandArgument='<%#Eval("PhotoID")%>' 
        OnClick="DelPhoto_Click">delete</asp:LinkButton> 
      </ItemTemplate> 

     </asp:DataList> 
    </ContentTemplate> 
</asp:UpdatePanel> 
<asp:EntityDataSource ID="PhotosEntityDataSource" runat="server" ContextTypeName="Misagh.DAL.MisaghSchoolEntities" 
    EnableDelete="True" EntitySetName="Photos" Where="it.AlbumID = @AlbumID"> 
    <WhereParameters> 
     <asp:ControlParameter ControlID="ddlAlbums" DefaultValue="0" DbType="Int32" PropertyName="SelectedValue" 
      Name="AlbumID" /> 
    </WhereParameters> 
</asp:EntityDataSource> 

我真的appriciated任何幫助

感謝

+0

能否請您出示的照片類代碼? – Andrei

回答

-1

您已經辦理OnItemDataBound。你可以簡單地做:

protected void PhotosDataList_ItemDataBound(Object sender, DataListItemEventArgs e) 
{ 
if (e.Item.ItemType == ListItemType.Item || 
      e.Item.ItemType == ListItemType.AlternatingItem) 
{ 
    // Retrieve the Label control in the current DataListItem. 
    Label albumName = (Label)e.Item.FindControl("lblAlbumName"); 
    albumName.Text = (e.Item.DataItem as ICustomTypeDescriptor).GetPropertyOwner(null).Album.Name; 
} 
} 

並設置EnableFlattening="true"在EntityDataSource聲明在您的標記

瞭解更多相關主題的here.