“当前”绑定到 EntityDataSource 的 asp:FormView 对象

发布于 2024-08-24 01:50:02 字数 698 浏览 13 评论 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>

...它从数据库返回 Order 类型的对象列表 (IEnumerable)。假设我的寻呼机位于第 2 页,因此 FormView 显示列表的第二个对象。

FormView 似乎“知道”它必须显示的对象,因为控件会

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

神奇地显示正确对象的“MyProperty”值。

如何在代码隐藏中访问此对象(Order 类型的实体作为一个整体,而不是使用“Eval”的单个属性)?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无畏 2024-08-31 01:50:02

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文