title: ASP.NET Web Forms (4.5) Strongly Typed Model Binding - DropDownList in InsertItemTemplate of ListView
body: <p><strong>Note: This is ASP.NET Web Forms Model Binding in .NET 4.5 and NOT MVC.</strong></p>
  
  <p>I am using the new Strongly Typed Model Binding features of ASP.NET Web Forms (4.5) to produce a list of items that can be edited. This is working fine for viewing the initial list, editing an item and deleting an item. I am however having a problem with the insertion of a new item.</p>
  
  <p>Specifically, within my EditItemTemplate and InsertItemTemplate I have a DropDownList (well, actually it is a custom control derived from DropDownList but for the purposes of this question it is a DropDownList). The control is defined within the markup as follows...</p>
  
  <pre><code>&lt;agp:ClientStatusDropDownList ID="ClientStatusID" runat="server"
  SelectedValue="&lt;%#: BindItem.ClientStatusID %&gt;" /&gt;
  </code></pre>
  
  <p>Within the EditItemTemplate, this is fine however within the InsertItemTemplate this generates an error upon running the page: <em>Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.</em></p>
  
  <p>As such, I removed the section <code>SelectedValue="&lt;%#: BindItem.ClientStatusID %&gt;"</code> from the InsertItemTemplate and tried again. This time no error message, however when the <code>ListView.InsertMethod</code> is called, the ClientStatusID property on the model is not set to the value of the DropDownList (whereas the rest of the properties are set correctly).</p>
  
  <p>The ListView.InsertMethod:</p>
  
  <pre><code>public void ListView_InsertMethod(int ID) {
  
    Model model = this.DbContext.Models.Create();
    if (this.TryUpdateModel(model)) {
      this.DbContext.SaveChanges();
      this.ListView.DataBind();
    }
  
  }
  </code></pre>
  
  <p>The Model class:</p>
  
  <pre><code>public class Model{
  
    public Int32 ID { get; set; }
    public String Description { get; set; }
    public Boolean IsScheduleFollowUp { get; set; }
    public Nullable&lt;Int32&gt; ClientStatusID { get; set; }
  
  }
  </code></pre>
  
  <p>The EditItemTemplate:</p>
  
  <pre><code>&lt;EditItemTemplate&gt;
    &lt;tr&gt;
      &lt;td&gt;
        &lt;asp:TextBox ID="Description" runat="server" Text="&lt;%#: BindItem.Description %&gt;" /&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;asp:CheckBox ID="IsScheduleFollowUp" runat="server" Checked="&lt;%# BindItem.IsScheduleFollowUp %&gt;" /&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;agp:ClientStatusDropDownList ID="ClientStatusID" runat="server" SelectedValue="&lt;%#: BindItem.ClientStatusID %&gt;" /&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;asp:Button ID="Update" runat="server" ClientIDMode="Static" CommandName="Update" Text="Update" /&gt;
        &lt;asp:Button ID="Cancel" runat="server" ClientIDMode="Static" CommandName="Cancel" Text="Cancel" /&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/EditItemTemplate&gt;
  </code></pre>
  
  <p>The InsertItemTemplate:</p>
  
  <pre><code>&lt;InsertItemTemplate&gt;
    &lt;tr&gt;
      &lt;td&gt;
        &lt;asp:TextBox ID="Description" runat="server" Text="&lt;%#: BindItem.Description %&gt;" /&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;asp:CheckBox ID="IsScheduleFollowUp" runat="server" Checked="&lt;%# BindItem.IsScheduleFollowUp %&gt;" /&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;agp:ClientStatusDropDownList ID="ClientStatusID" runat="server" /&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;asp:Button ID="Insert" runat="server" ClientIDMode="Static" CommandName="Insert" Text="Add" /&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/InsertItemTemplate&gt;
  </code></pre>
  
  <p>I had originally thought that it was the ID of the control that was used to determine the property on the model that the value would be passed to (i.e. where the TextBox was called "Description", the value would be passed to the "Description" property of the model). Clearly this is not the case and it is instead controlled by the "&lt;%# BindItem.Description %>", however as you can see from the rest of this question I am unable to use this syntax in the "InsertItemTemplate". I cannot believe that the DropDownList is not supported in this scenario, but I cannot find any examples of a DropDownList being used with the 4.5 model bindings using Google or Bing (in fact, there are very few examples of the new model binding in ASP.NET 4.5 using anything other than a couple of TextBox controls).</p>
  
  <p>Can anybody shed any further light on the issue (and preferably tell me what needs to be done)?</p>
  
  <p>Other questions on SO that I have looked at...</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/470315/binding-a-dropdownlist-in-listview-insertitemtemplate-throwing-an-error">Binding a DropDownList in ListView InsertItemTemplate throwing an error</a></li>
  <li><a href="https://stackoverflow.com/questions/2184757/asp-net-listview-with-identical-markup-in-edititemtemplate-and-insertitemtemplat">ASP.NET ListView with identical markup in EditItemTemplate and InsertItemTemplate</a></li>
  <li><a href="https://stackoverflow.com/questions/2265852/bind-selectedvalue-of-asp-net-dropdownlist-to-custom-object">Bind SelectedValue of ASP.net DropDownList to custom object</a></li>
  <li><a href="https://stackoverflow.com/questions/3785021/databinding-to-asp-net-dropdownlist-list-in-listview">Databinding to ASP.NET DropDownList list in ListView</a></li>
  </ul>
  
  <p>All of these use the older style binding methods and not the new methods in 4.5</p>
  
  <p>Thanks.</p>