【问题标题】:How do I create a nested GridView bound to parent's EntityDataSource's navigation property?如何创建绑定到父 EntityDataSource 导航属性的嵌套 GridView?
【发布时间】:2009-06-14 02:57:29
【问题描述】:
<asp:FormView DataSourceId="edsAccounts">
    <ItemTemplate>
        <asp:TextBox Text='<%# Eval("Email") %>' />
        <asp:DataGrid ID="dgReports" DataSource='<%# Eval("Reports") %>'>
    </ItemTemplate>
</asp:FormView>
<asp:EntityDataSource ID="edsAccounts" runat="server" ConnectionString="name=Entities" DefaultContainerName="Entities" EntitySetName="Accounts" EntityTypeFilter="Account" Include="Reports" />

我希望 dgReports 开始工作。 请注意,电子邮件文本框可以正常工作。

【问题讨论】:

    标签: asp.net entity-framework databound-controls entitydatasource navigation-properties


    【解决方案1】:

    我确实创建了单独的内部数据源,但我遇到了另一个问题。 我无法将 Where 子句设置为父实体的 ID。

    注意 FormView.DataItem 不可访问;它是 EntityDataSourceWrapper 类型,它是一个友元类并且不可访问。

    所以我创建了一个函数来通过反射来处理它。

    我认为这是 Microsoft 的错误,在他们修复之前,以下内容可能对使用嵌套 EntityDataSource 控件的任何人有用。

    这里是:

    Module Functions
        Public Function GetEntity(Of TEntity As EntityObject)(ByVal entityDataSourceWrapper As Object) As TEntity
            If entityDataSourceWrapper Is Nothing Then Return Nothing
            Dim type = entityDataSourceWrapper.GetType()
            If Not type.FullName = "System.Web.UI.WebControls.EntityDataSourceWrapper" Then Return Nothing
            Dim wrapper = type.GetProperty("WrappedEntity", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)
            Return DirectCast(wrapper.GetValue(entityDataSourceWrapper, Nothing), TEntity)
        End Function
    End Module
    

    现在在后面的代码中我执行以下操作:

    Protected Sub fvAccounts_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles fvAccounts.DataBound       
        If fvAccounts.CurrentMode <> FormViewMode.ReadOnly Then Exit Sub
        Dim account As Account = GetEntity(Of Account)(fvAccounts.DataItem)
        If account Is Nothing Then Exit Sub
        Dim edsReports As EntityDataSource = fvAccounts.Row.FindControl("edsReports")
        edsReports.Where = "it.Account.AccountId = " & account.AccountId
        gvReports.DataBind()
    End Sub
    

    注意模型中的层次结构:帐户有很多报告。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-10
      • 2010-11-01
      • 1970-01-01
      • 2014-08-06
      • 2012-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多