0

ページングが有効になっているFormViewがあります。FormViewはEntityDataSourceにバインドされています...

<asp:EntityDataSource ID="MyEntityDataSource" runat="server" 
    ConnectionString="name=MyEntitiesContext" 
    DefaultContainerName="MyEntitiesContext" 
    EntitySetName="Order" 

    // ... more stuff to define a query

</asp:EntityDataSource>

...データベースからタイプのオブジェクトのリスト(IEnumerable)を返しOrderます。たとえば、私のポケットベルが2ページ目に配置されているため、FormViewはリストの2番目のオブジェクトを表示します。

FormViewは、次のようなコントロールがあるため、表示する必要のあるオブジェクトを「認識」しているようです。

<asp:Label ID="MyLabel" runat="server" Text='<%# Eval("MyProperty")%>'/>

正しいオブジェクトの「MyProperty」の値を魔法のように表示します。

Orderコードビハインドでこのオブジェクト(「Eval」を使用して単一のプロパティではなく、タイプ全体のエンティティ)にアクセスするにはどうすればよいですか?

4

1 に答える 1

5

FormViewのDataBoundイベントハンドラーで、次のことができます。

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

ここで、.WrappedEntity()は、次のように定義された拡張メソッドです。

/// <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;
    }
}

これらの例ではEFエンティティAdvertを使用していますが、たとえばOrderに置き換えます。

http://www.dontcodetired.com/blog/post/Accessing-Entity-Framework-Entity-In-EntityDataSource-Data-Bound-Controls.aspx

マークアップとコードの完全な例:

<%@ 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>

背後にあるコード:

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;
    }
}
于 2010-03-04T20:39:45.993 に答える