我确实创建了独立的内部数据源,但我又遇到了另一个问题。 我无法将Where子句设置为父实体的ID。
请注意,FormView.DataItem不可访问;它是EntityDataSourceWrapper类型,它是一个朋友类,不可访问。
所以我创建了一个函数来处理它的反射。
我认为这是一个微软的bug,直到他们解决了这个问题,对于任何使用嵌套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
注意模型的层次结构:帐户有很多报道。