views:

491

answers:

1

I have been searching around to find the best option of doing this.

Basically, I want to do two way databinding of multiple controls (textbox, dropdownlist, checkbox, etc) to a single instance of custom class/entity, ie: Person.

It seems, the (only?) way to do this is using an like so:

<asp:FormView ID="FormView1" runat="server" DataKeyNames="OrderID"
        DataSourceID="SqlDataSource1" DefaultMode="Edit">
        <EditItemTemplate>
          OrderID:
          <asp:Label ID="OrderIDLabel1" runat="server" Text='<%# Eval("OrderID") %>' />
          <br />
          CustomerID:
          <asp:DropDownList ID="CustomerIDDropDownList" runat="server"
            DataValueField='CustomerID' DataSourceID="CustomerDataSource"
            DataTextField="CompanyName"
            SelectedValue='<%# Bind("CustomerID") %>'
            />
          EmployeeID:
          <asp:TextBox ID="EmployeeIDTextBox" runat="server"
            Text='<%# Bind("EmployeeID") %>' />
          <br />

Some issues:
- This is limited to using an ObjectDataSource control (ie: can't just use an instance of the desired class in the code behind)
- Forces you to define a second (likely identical layout) read only template....would be nice to have some some mechanism that could intelligently render a read-only view derived from the edit template.
- The binding declaration Text='<%# Bind("EmployeeID") %>' is loosely typed, so vulnerable to spelling errors - etc

So my first question I guess is, is an asp:FormView the only way in ASP.Net to do declarative databinding of a single entity?

Secondly, how feasible would it be to hand roll some sort of a two way binding mechanism? I guess it would have to be reflection based, but I could live with that. Anyone recommendations on how one would declare the binding relationships in the aspx page? Would the proper way be like:
Text='<%# MySuperDuperBind("EmployeeID") %>'

And then somewhere (where?) my MySuperDuperBind implementation will get called as the page is rendered....how this is done is a bit beyond me though. And if I want to render in readonly, I can call a secondary function that will remove the editable UI control from the form and replace it with the corresponding read only version (ie: a Textbox is replaced with a Label).

Another alternative route is getting away from webforms and going to a client side templating solution such as this very nice looking solution:
http://weblogs.asp.net/dwahlin/archive/2009/05/03/using-jquery-with-client-side-data-binding-templates.aspx

However, I have no clue how to write the asp.net webservices properly in order to retrieve and save data in this type of an architecture.

Ideas?

+1  A: 

is an asp:FormView the only way in ASP.Net to do declarative databinding of a single entity?

There's also DetailsView but it has the same issues.

I've mostly given up on 2-way databinding. It's great for prototyping and gets me 80-90% of the way to a complete solution but the last 10-20% is a nightmare. Binding any non-trivial object always seems to involve so many event handlers to customize behavior that it feels like spaghetti code to me.

I usually have two methods:

MapEntityToView(entity)
MapViewToEntity(entity)

that I call to display the entity and to populate from the page, respectively. It can be tedious to write but I don't have to wrestle with data binding issues.

I do use 1-way binding extensively for read only pages and displaying items in list controls.

Jamie Ide
Sadly, I am marking yours as the correct answer. See also: http://stackoverflow.com/questions/2435185/not-possible-to-load-dropdownlist-on-formview-from-code-behind and http://stackoverflow.com/questions/2470533/automapper-a-viable-alternative-to-two-way-databinding-using-a-formview
tbone