2009-07-01 4 views
1

私は心を失いつつあります。それは単純なシナリオです:FormViewで使用されるすべてのEntityプロパティがEntityDataSourceによってロードされているわけではありません。

のaspxファイル内のエンティティ定義(生成)

// 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> 

...

// ... 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 
} 

基本的には専用のプロパティの背後にあるコードでフォームビューでバインドされている値は、Updateイベントハンドラで利用できます。キーでe.Contextからエンティティをリロードすると、それらのプロパティだけが再び取得されます。

何がありますか? Updatingイベントでエンティティのすべてのプロパティにアクセスするにはどうすればよいですか? また、データソースにインクルードが含まれている場合、それらの値はUpdatingイベントで使用できません。何が起こっているの、助けてください!

答えて

3

これはまた、私が歩んでこれの理由を理解するまで私を挫折させました。

ここに私が見つけたものがあります。プロパティは、オブジェクトにバインドされている場合にのみ、ビューステートに格納されます。往復では、格納されたプロパティのみがビューステートからエンティティオブジェクトに復元されます。これは、実際に使用されるデータだけが実際に利用可能となるため意味をなさなうことができます。しかし、これは、コードの背後にあるエンティティオブジェクトの操作を考慮していません。あなたが代わりにバインドの評価を使用する場合、これがまた起こる:

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

ここで私が使用して回避策です。コードビハインドでエンティティオブジェクトプロパティの1つを使用する場合は、EditItemTemplateの非表示フィールドにバインドします。私がこれをしたら、プロパティはエンティティオブジェクトに復元され、コードビハインドで利用可能になりました。

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

私はちょっと後で自分自身を考え出しました。私はそれが吸うと思うが、まあまあ。あなたはカルマを手に入れなければならないので、答えを探している他の人はそれを得ることができます。乾杯! – Strelok

関連する問題