2009-07-01 1 views
1

나는 내 마음을 잃고 있습니다.FormView에서 사용되는 EntityDataSource에 의해로드 된 모든 Entity 속성이 없음

// ... as per Diego Vega's unwrapping code 
public TEntity GetItemObject<TEntity>(object dataItem) 
    where TEntity : class 
{ 
    var entity = dataItem as TEntity; 
    if (entity != null) 
    { 
     return entity; 
    } 
    var td = dataItem as ICustomTypeDescriptor; 
    if (td != null) 
    { 
     return (TEntity)td.GetPropertyOwner(null); 
    } 
    return null; 
} 
protected void UpdatingEntity(object sender, EntityDataSourceChangingEventArgs e) { 
    SomeEntity entity = GetItemObject<SomeEntity>(e.Entity); 
    // at this point entity.Property1 has the value of whatever was entered in the 
    // bound text box, but entity.Property2 is null (even when the field bound to this 
    // property in the database contains a value 
    // if I add another textbox to form view and bind Property2 to it ... then I can 
    // obviously get its value from the entity object above 
} 
기본적으로

속성 만 뒤에 코드 (생성)

엔티티 정의

// this class is obviously generated by the designer, this is just an example 
public class SomeEntity { 
    public int SomeEntityID { get; set; } // primary key 
    public String Property1 { get; set; } 
    public String Property2 { get; set; 
} 

.. 영문 파일

<asp:EntityDataSource 
ID="EntityDataSource1" runat="server" 
ConnectionString="name=Connection" DefaultContainerName="SomeEntities" 
EnableDelete="True" EnableInsert="True" EnableUpdate="True" 
EntitySetName="SomeEntity" OnUpdating="UpdatingEntity" AutoGenerateWhereClause="true"> 
<WhereParameters> 
    <asp:QueryStringParameter Name="SomeEntityID" QueryStringField="SomeEntityID" Type="Int32"/> 
</WhereParameters> 
</asp:EntityDataSource> 
<asp:FormView runat="server" ID="FormView1" DataSourceID="EntityDataSource1"> 
    <EditItemTemplate> 
    <asp:TextBox runat="server" ID="textbox1" Text='<%# Bind("Property1")%>' 
    </EditItemTemplate> 
</asp:FormView> 

의 : 그것은 간단한 시나리오입니다 폼 뷰에 바인딩 된 값을 Update 이벤트 핸들러에서 사용할 수 있습니다. 키로 e.Context에서 엔티티를 다시로드하면 해당 속성 만 다시 가져옵니다.

무엇을 제공합니까? Updating 이벤트에서 엔티티의 모든 속성에 액세스하려면 어떻게해야합니까? 또한 데이터 소스에 포함이 포함 된 경우 포함 된 값은 업데이트 이벤트에서 사용할 수 없습니다. 무슨 일이 일어나는지, 제발 도와주세요!

답변

3

나는 뒤로 물러나서 이것에 대한 이유를 깨달을 때까지 나를 좌절시켰다.

다음은 내가 찾은 것입니다. 속성은 객체에 바인딩 된 경우에만 뷰 상태에 저장됩니다. 왕복 여행에서는 저장된 상태의 속성 만 뷰 상태에서 엔터티 개체로 복원됩니다. 실제로 작업중인 데이터 만 사용할 수 있기 때문에 의미가 있습니다. 그러나 이것은 코드 배후에서 엔티티 객체의 조작을 고려하지 않습니다. 이것은 Bind 대신 Eval을 사용하는 경우에도 발생합니다.

<asp:TextBox runat="server" ID="textbox1" Text='<%# Eval("Property1")%>' 

다음은 내가 사용하는 해결 방법입니다. 코드 숨김에서 엔터티 개체 속성 중 하나를 사용하려면 EditItemTemplate의 숨겨진 필드에 바인딩합니다. 이렇게하면 속성이 엔터티 개체에 복원되고 코드 숨김에서 사용할 수 있습니다.

<asp:HiddenField ID="HiddenField1" runat="server" value='<%# Bind("Property2") %>'/> 
+0

네, 나중에 나 자신을 알아 냈습니다. 나는 그것이 싫증 난다라고 생각한다. 그러나 오 오 잘. 카르마를 얻어야하므로 다른 사람들이 답을 구할 수 있습니다. 건배! – Strelok

관련 문제