title: Two way databinding in ASP.NET
body: <p>Let say that we have an object</p>
  
  <pre><code>class Entity
  {
      public string ID {get; set;}
      public string Name {get; set;}
  }
  </code></pre>
  
  <p>I want to bind properties to two textboxes on a page something like this:</p>
  
  <pre><code>&lt;asp:FormView ID="FormView" runat="server"&gt;
    &lt;ItemTemplate&gt;
      &lt;asp:textbox ID="TextId" Text='&lt;%# Bind("ID") %&gt;'/&gt;
      &lt;asp:textbox ID="TextId" Text='&lt;%# Bind("Name") %&gt;'/&gt;
    &lt;/ItemTemplate&gt;
  &lt;/asp:FormView&gt;
  </code></pre>
  
  <p>and then write this in code behind</p>
  
  <pre><code>public EntityObject
  {
      get { return ViewState["Entity"] as Entity; }
      set { ViewState["Entity"] = value; }
  }
  
  protected override void OnInit(EventArgs e)
  {
      if (EntityObject== null)
          EntityObject= new EntityObject();
  
      FormView.DataSource = new[] { EntityObject };
      FormView.DataBind();
      base.OnInit(e);
  }
  </code></pre>
  
  <p>And when I enter values in textboxes I expect EntityObject to have these values in properties when page reloads after PostBack, but properties are always null.</p>