title: ASP.Net - Two way databinding of a single entity - Options, best way...roll your own?
body: <p>I have been searching around to find the best option of doing this.</p>
  
  <p>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.</p>
  
  <p>It <em>seems</em>, the (only?) way to do this is using an  like so:</p>
  
  <pre><code>&lt;asp:FormView ID="FormView1" runat="server" DataKeyNames="OrderID"
          DataSourceID="SqlDataSource1" DefaultMode="Edit"&gt;
          &lt;EditItemTemplate&gt;
            OrderID:
            &lt;asp:Label ID="OrderIDLabel1" runat="server" Text='&lt;%# Eval("OrderID") %&gt;' /&gt;
            &lt;br /&gt;
            CustomerID:
            &lt;asp:DropDownList ID="CustomerIDDropDownList" runat="server"
              DataValueField='CustomerID' DataSourceID="CustomerDataSource"
              DataTextField="CompanyName"
              SelectedValue='&lt;%# Bind("CustomerID") %&gt;'
              /&gt;
            EmployeeID:
            &lt;asp:TextBox ID="EmployeeIDTextBox" runat="server"
              Text='&lt;%# Bind("EmployeeID") %&gt;' /&gt;
            &lt;br /&gt;
  </code></pre>
  
  <p>Some issues:<br>
   - This is limited to using an ObjectDataSource control (ie: can't just use an instance of the desired class in the code behind)<br>
   - 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.<br>
   - The binding declaration Text='&lt;%# Bind("EmployeeID") %>' is loosely typed, so vulnerable to spelling errors
   - etc  </p>
  
  <p>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?</p>
  
  <p>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:<br>
   Text='&lt;%# MySuperDuperBind("EmployeeID") %>'</p>
  
  <p>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).</p>
  
  <p>Another alternative route is getting away from webforms and going to a client side templating solution such as this very nice looking solution:<br>
  <a href="http://weblogs.asp.net/dwahlin/archive/2009/05/03/using-jquery-with-client-side-data-binding-templates.aspx" rel="nofollow noreferrer">http://weblogs.asp.net/dwahlin/archive/2009/05/03/using-jquery-with-client-side-data-binding-templates.aspx</a></p>
  
  <p>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.</p>
  
  <p>Ideas?</p>