如何在gridview控件中显示导航属性?

时间:2011-10-16 10:00:32

标签: asp.net navigation

我想在网格视图控件中显示导航属性的字段值。 我的实体是专辑和照片,而照片实体必须与专辑实体相关联。

我想显示相册的名称,但它从未显示在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>

我真的会帮助任何帮助

由于

1 个答案:

答案 0 :(得分:-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;
 }
}

在标记

中的EntityDataSource声明中设置EnableFlattening="true"

详细了解相关主题here.