views:

476

answers:

1

I am losing my mind. It's a simple scenario:

Entity definition (generated)

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

.. in aspx file

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

... in code behind

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

Basically ONLY the properties bound in the form view have their values available in the Updating event handler, worse still when I try to reload the entity from e.Context by key, I get only those properties again.

What gives? How can get access to all the properties of the entity in the Updating event? Also if the datasource contains Include, those included values are NOT available in the Updating event. What's going on, please HELP!

+2  A: 

This frustrated me also until I stepped back and realized the reason for this.

Here's what I found. The properties are stored in the view state only if they are bound to an object. On the round trip, only those stored properties are restored from the view state to the entity object. It can make sense since only the data being worked with will actually be available. However, this does not take into account any manipulation of the entity object in the code behind. This also happens if you use Eval instead of Bind:

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

Here's the work-around I use. If I am going to use one of the entity object properties in the code-behind, I bind it to a hidden field in the EditItemTemplate. Once I did this, the properties were restored to the entity object and available in the code-behind.

<asp:HiddenField ID="HiddenField1" runat="server" value='<%# Bind("Property2") %>'/>
gbenes
Yep, I kinda figured it out myself later on. I think it sucks, but oh well. You should get the karma, so other people looking for an answer can get it. Cheers!
Strelok