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

发布于 2024-12-10 04:08:35 字数 2151 浏览 0 评论 0原文

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

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

我真的很感谢任何帮助,

谢谢

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">
    </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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聽兲甴掵 2024-12-17 04:08:35

您已经处理了 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"

阅读有关相关主题的更多信息 此处。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文