부모의 EntityDatasource의 내비게이션 속성에 묶인 중첩 그리드 뷰를 어떻게 만들려면?
문제
<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가 작동을 시작하기를 원합니다. 이메일 텍스트 상자는 잘 작동합니다.
해결책
나는 실제로 별도의 내부 데이터 소스를 만들었지 만 또 다른 문제가 생겼습니다. 나는 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
모델의 계층 구조에주의하십시오. 계정에는 많은 보고서가 있습니다.
제휴하지 않습니다 StackOverflow