.NET 4 ASP.NET I have a DetailsView that displays an entity structure record for a...">

Asp.net entity framework <% # Bind ("linkedTable.Field")%>

.NET 4 ASP.NET

I have a DetailsView that displays an entity structure record for a table that has an associated lookup table. I have an asp: BoundField with data field set to "linkedTable.Field" and it displays the value.

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

      

I am trying to use this value in asp: TemplateField, but when I try to use it:

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

      

Nothing is visible on the label. I can change Bind () to a field that is not part of the linked table and it works (ie the "ID" field). My problem is that I don't understand why the associated value is. The field value appears in one context and not in another.

FYI, my data connection is 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

+3


source share


1 answer


Ok, found a problem:
Need to add Include="linkedTable"

to tag EntityDataSource

. Still not sure why it even worked in the tag <asp:DataBound />

.

Answer source: forums.asp.net

Copy of text:



you should start here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.entitydatasource.include.aspx

note that you will not be able to bind (I mean bind to two-way bind) related objects (see notes there).

and you will have to use TemplateField for those properties.

see this example (I used the "TableAB" reference table for the 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 will need to re-consider updates and deletions, you can do them manually.

+1


source







All Articles