I have a FormView with paging enabled. The FormView is bound to an EntityDataSource …
<asp:EntityDataSource ID="MyEntityDataSource" runat="server"
ConnectionString="name=MyEntitiesContext"
DefaultContainerName="MyEntitiesContext"
EntitySetName="Order"
// ... more stuff to define a query
</asp:EntityDataSource>
… which returns a list (IEnumerable) of objects of type Order from a database. Let’s say, my pager is positioned on page 2, so the FormView displays the second object of the list.
The FormView seems to “know” the object it has to display since controls like
<asp:Label ID="MyLabel" runat="server" Text='<%# Eval("MyProperty")%>'/>
magically display the value of “MyProperty” of the correct object.
How can I access this object (the entity of type Order as a whole, not single properties by using “Eval”) in Code-behind?
in the DataBound event handler for your FormView you can do:
Where .WrappedEntity() is an extension method defined as:
These examples use the EF entity Advert, but you would replace with Order for example.
http://www.dontcodetired.com/blog/post/Accessing-Entity-Framework-Entity-In-EntityDataSource-Data-Bound-Controls.aspx
Full example markup and code:
Code behind: