Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 645421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:30:01+00:00 2026-05-13T21:30:01+00:00

I have a FormView with paging enabled. The FormView is bound to an EntityDataSource

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:30:01+00:00Added an answer on May 13, 2026 at 9:30 pm

    in the DataBound event handler for your FormView you can do:

    Advert ad = FormView1.DataItem.WrappedEntity<Advert>();
    

    Where .WrappedEntity() is an extension method defined as:

    /// <summary>
    /// Gets the actual EF entity object that is being wrapped and databound.
    /// </summary>
    /// <example>
    /// Advert ad = myFormView.DataItem.WrappedEntity<Advert>();
    /// (where myFormView is databound to EntityDataSource returning Advert entity)
    /// </example>
    static class WrappedEFEntityExtensions
    {
        public static TEntity WrappedEntity<TEntity>(this object dataItem) where TEntity : class
        {
            var entity = dataItem as TEntity;
    
            if (entity != null)
                return entity;
    
            var typeDescriptor = dataItem as ICustomTypeDescriptor;
    
            if (typeDescriptor != null)
                return (TEntity)typeDescriptor.GetPropertyOwner(null);
    
            return null;
        }
    }
    

    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:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="stackOF.aspx.cs" Inherits="stackOF" %>
    <%@ Register Assembly="System.Web.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        Namespace="System.Web.UI.WebControls" TagPrefix="asp" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>    
            <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
                ConnectionString="name=mototradeEntities" 
                DefaultContainerName="mototradeEntities" EntitySetName="Adverts">
            </asp:EntityDataSource>
    
    
            <asp:FormView ID="FormView1" runat="server" AllowPaging="True" 
                DataKeyNames="ID" DataSourceID="EntityDataSource1" 
                ondatabound="FormView1_DataBound">                        
                <ItemTemplate>
                    ID:<asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                    <%-- other properties here --%>
                </ItemTemplate>
            </asp:FormView>    
        </div>
            <asp:Label ID="lblTest" runat="server" Text="Label"></asp:Label>
        </form>
    </body>
    </html>
    

    Code behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    using mototradeModel;
    
    public partial class stackOF : System.Web.UI.Page
    {
         protected void FormView1_DataBound(object sender, EventArgs e)
        {
            Advert ad = FormView1.DataItem.WrappedEntity<Advert>();
            if (ad != null)
            {
                lblTest.Text = "current object databound to FormView1: " + ad.ID;
            }
        }
    }
    
    /// <summary>
    /// Gets the actual EF entity object that is being wrapped and databound.
    /// </summary>
    /// <example>
    /// Advert ad = myFormView.DataItem.WrappedEntity<Advert>();
    /// (where myFormView is databound to EntityDataSource returning Advert entity)
    /// </example>
    static class WrappedEFEntityExtensions
    {
        public static TEntity WrappedEntity<TEntity>(this object dataItem) where TEntity : class
        {
            var entity = dataItem as TEntity;
    
            if (entity != null)
                return entity;
    
            var typeDescriptor = dataItem as ICustomTypeDescriptor;
    
            if (typeDescriptor != null)
                return (TEntity)typeDescriptor.GetPropertyOwner(null);
    
            return null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.