How can i display a navigation property in a gridview control?
I want to display a field value from a navigation property in a grid view control. My entities are Albums and Photos and a Photo entity must be associated with an Album entity.
I want to display an albums’s name but it is never shown in the gridview control.
The following is my markup:
<h4>
<asp:DropDownList ID="ddlAlbums" runat="server" Style="padding: 3px; width: 150px;"
AutoPostBack="True">
&l开发者_如何学Ct;/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>
<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>
I would really appriciated any help
Thanks
You already handle OnItemDataBound. You can simply do:
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;
}
}
And set EnableFlattening="true"
in your EntityDataSource declaration in your markup
Read more about a related topic here.
精彩评论