asp.net实体框架<%#Bind("linkedTable.Field")%> [英] asp.net entity framework <%# Bind("linkedTable.Field") %>

查看:81
本文介绍了asp.net实体框架<%#Bind("linkedTable.Field")%>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET 4 ASP.NET

.NET 4 ASP.NET

我有一个DetailsView,它显示具有链接查找表的表的实体框架记录.我有一个asp:BoundField,其数据字段设置为"linkedTable.Field",它显示了一个值.

I have a DetailsView that is displaying an entity framework record for a table that has a linked lookup table. I have an asp:BoundField with the datafield set as "linkedTable.Field" and it displays a value.

<asp:BoundField DataField="linkedTable.Field" HeaderText="linkedTable.Field" 
            SortExpression="linkedTable.Field" />

我试图在asp:TemplateField中使用该值,但是当我尝试使用以下方法获取它时:

I am trying to use that value in an asp:TemplateField but when I try to get it using:

<asp:TemplateField HeaderText="Field" SortExpression="linkedTable.Field" >
   <EditItemTemplate>
     <asp:Label runat="server" ID="lblField" Text='<%# Bind("linkedTable.Field") %>' />
   </EditItemTemplate>
</asp:TemplateField>

标签上没有任何内容.我可以将Bind()更改为不属于链接表的字段并且它可以工作(即"ID"字段).我的问题是我不明白为什么linkedtable.Field值显示在一个上下文中而不显示在另一个上下文中.

Nothing shows up in the label. I can change the Bind() to a field that is not part of the linked table and it works (i.e. the "ID" field). My problem is I don't understand why the linkedtable.Field value shows up in one context and not in the other.

仅供参考,我的数据连接是EntityDataSource

FYI, my data connection is a EntityDataSource

<asp:EntityDataSource ID="edsNYSEDaily" runat="server" 
    ConnectionString="name=ServerDBEntities" 
    DefaultContainerName="ServerDBEntities" EntitySetName="tblNYSE" 
    EntityTypeFilter="tblNYSE" EnableUpdate="True" EnableFlattening="true"
    AutoGenerateWhereClause="True" Select="" Where="">
    <WhereParameters>
        <asp:QueryStringParameter DefaultValue="0" Name="ID" 
            QueryStringField="ID" Type="Int32" />
    </WhereParameters>

如果您需要其他任何信息,请告诉我.我被困住了

Let me know if you need any other information. I am stuck

推荐答案

好,发现了问题:
需要向 EntityDataSource 标签添加 Include ="linkedTable" .仍然不确定为什么它甚至可以在< asp:DataBound/> 标记中运行.

Ok, discovered the problem:
Needed to add Include="linkedTable" to the EntityDataSource tag. Still not sure why it was even working in the <asp:DataBound /> tag.

答案来源: forums.asp.net

文本副本:

您应该从这里开始:http://msdn.microsoft.com/zh-CN/library/system.web.ui.webcontrols.entitydatasource.include.aspx

请注意,您将无法绑定(我的意思是双向数据绑定)相关实体(请参见此处的说明).

notice that, you won't be able to Bind (I mean two-way data-binding) the related entities (see the remarks there).

,并且您必须为这些属性使用TemplateField.

and, you'd have to use a TemplateField for those properties.

请参见以下示例(我将链接表'TableAB'用于EntitySetName并包括了其他两个):

see this example (I used the link table 'TableAB' for EntitySetName and included the other two):

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
        AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="EntityDataSource1">
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
            <asp:BoundField DataField="IDA" HeaderText="IDA" SortExpression="IDA" />
            <asp:BoundField DataField="IDB" HeaderText="IDB" SortExpression="IDB" />
            <asp:TemplateField HeaderText="TableA Name">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("TableA.NameA") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="TableB Name">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("TableB.NameB") %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=ABLinkEntities"
        DefaultContainerName="ABLinkEntities" EnableDelete="True" EnableFlattening="False"
        EnableInsert="True" EnableUpdate="True" EntitySetName="TableABs" Include="TableA,TableB">
    </asp:EntityDataSource>

您必须重新考虑更新和删除,可以手动进行.

you'll have to re-consider the updates and deletes, you could do them manually.

这篇关于asp.net实体框架&lt;%#Bind("linkedTable.Field")%&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆