title: How to Edit data in nested Listview
body: <p>I am using listview to display a list of items and a nested listview to show list of features to each item. Both parent and child listview need to able Insert,Edit and delete operation. It works fine for parent listview. But when I try to edit an child item, The edit button does not take it into Edit mode. Can you please suggest me what I am missing in my code?</p>
  
  <pre><code>&lt;asp:ListView ID="lvParent" runat="server"                 
                  OnItemDataBound="lvParent_ItemDataBound" 
                  onitemcanceling="lvParent_ItemCanceling" onitemcommand="lvParent_ItemCommand" 
                  DataKeyNames="ItemID" onitemdeleting="lvParent_ItemDeleting" 
                  oniteminserting="lvParent_ItemInserting"  &gt;
                  &lt;LayoutTemplate&gt;                                        
                      &lt;asp:PlaceHolder ID="itemPlaceholder" runat="server"&gt;&lt;/asp:PlaceHolder&gt;
                      &lt;div align="right"&gt;
                          &lt;asp:Button ID="btnInsert" runat="server" Text="ADD Item" onclick="btnInsert_Click"/&gt;
                      &lt;/div&gt;
                  &lt;/LayoutTemplate&gt;
                  &lt;ItemTemplate&gt;
                      &lt;table runat="server" cellpadding="0" cellspacing="0" border="0"  width="100%"&gt;
                          &lt;tr&gt;
                              &lt;td&gt;
                                  &lt;div id="dvDetail"&gt;
                                      &lt;span &gt;Description&lt;/span&gt;
  
                                      &lt;asp:TextBox ID="txtDescription" runat="server" Text='&lt;%# DataBinder.Eval(Container.DataItem, "Description") %&gt;' TextMode="MultiLine" &gt;&lt;/asp:TextBox&gt;
  
                                  &lt;/div&gt;                                
                                  &lt;div id="dvFeature" &gt;
                                      &lt;span&gt;Feature List&lt;/span&gt;                                                                  
                                      &lt;asp:ListView ID="lvChild" runat="server"  
                                          InsertItemPosition="LastItem" 
                                          DataKeyNames="FeatureID" OnItemCommand="lvChild_ItemCommand"
                                           OnItemCanceling="lvChild_ItemCanceling" OnItemDeleting="lvChild_ItemDeleting" 
                                           OnItemEditing="lvChild_ItemEditing" OnItemInserting="lvChild_ItemInserting" OnItemUpdating="lvChild_ItemUpdating"
                                          DataSource='&lt;%# DataBinder.Eval(Container.DataItem, "FeatureList") %&gt;' &gt;
                                          &lt;LayoutTemplate&gt;
                                              &lt;ul &gt;
                                                  &lt;asp:PlaceHolder runat="server" ID="itemPlaceHolder" &gt;&lt;/asp:PlaceHolder&gt;                                                                                                                               
                                              &lt;/ul&gt;   
                                          &lt;/LayoutTemplate&gt;
                                          &lt;ItemTemplate&gt;
                                              &lt;li&gt;
                                                  &lt;span class="dvList"&gt;&lt;%# DataBinder.Eval(Container.DataItem, "FeatureTitle")%&gt;&lt;/span&gt;                                                
  
                                                  &lt;div class="dvButton" &gt;
                                                      &lt;asp:ImageButton ID="btnEdit" runat="server" ImageUrl="/Images/edit_16x16.gif" AlternateText= "Edit" CommandName="Edit" CommandArgument='&lt;%# DataBinder.Eval(Container.DataItem, "FeatureID") %&gt;' Width="12" Height="12" /&gt;
                                                      &lt;asp:ImageButton ID="btnDelete" runat="server" ImageUrl="/Images/delete_16x16.gif" AlternateText= "Delete" CommandName="Delete" CommandArgument='&lt;%# DataBinder.Eval(Container.DataItem, "FeatureID") %&gt;' Width="12" Height="12" /&gt;
  
                                                  &lt;/div&gt;
  
                                              &lt;/li&gt;
  
                                          &lt;/ItemTemplate&gt;
                                          &lt;EditItemTemplate&gt;
                                              &lt;li&gt;
                                                  &lt;asp:TextBox ID="txtFeature"  Text='&lt;%# DataBinder.Eval(Container.DataItem, "FeatureTitle")%&gt;' runat="server"&gt;&lt;/asp:TextBox&gt;
  
                                                  &lt;div class="dvButton"&gt;
                                                      &lt;asp:ImageButton ID="btnUpdate" runat="server" ImageUrl="/Images/ok_16x16.gif" AlternateText= "Update" CommandName="Update" CommandArgument='&lt;%# DataBinder.Eval(Container.DataItem, "FeatureID") %&gt;' Width="12" Height="12" /&gt;
                                                      &lt;asp:ImageButton ID="btnCancel" runat="server" ImageUrl="/Images/delete_16x16.gif" AlternateText= "Cancel" CommandName="Cancel" Width="12" Height="12" CausesValidation="false" /&gt;
  
                                                  &lt;/div&gt;
  
                                              &lt;/li&gt;
                                          &lt;/EditItemTemplate&gt;
                                          &lt;InsertItemTemplate&gt;                                            
                                                  &lt;asp:TextBox ID="txtFeature" runat="server"&gt;&lt;/asp:TextBox&gt;
  
                                                  &lt;div class="dvButton"&gt;
                                                      &lt;asp:ImageButton ID="btnInsert" runat="server" ImageUrl="/Images/ok_16x16.gif" AlternateText= "Insert" CommandName="Insert" Width="12" Height="12" /&gt;
                                                      &lt;asp:ImageButton ID="btnCancel" runat="server" ImageUrl="/Images/delete_16x16.gif" AlternateText= "Cancel" CommandName="Cancel" Width="12" Height="12" CausesValidation="false" /&gt;
  
                                                  &lt;/div&gt;
  
                                          &lt;/InsertItemTemplate&gt;
                                      &lt;/asp:ListView&gt;
                                  &lt;/div&gt;                                
                              &lt;/td&gt;
  
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td align="right"&gt;
                                  &lt;div id="dvButton" &gt;
                                      &lt;asp:Button ID="btnSave" runat="server"  Text="Save" 
                                          CommandName="Save"  
                                          CommandArgument='&lt;%# DataBinder.Eval(Container.DataItem, "ItemID")  %&gt;' /&gt;
                                      &lt;asp:Button ID="btnDelete" runat="server" Text="Delete"  CssClass="Cancel" 
                                          CommandName="Delete" 
                                          CommandArgument='&lt;%# DataBinder.Eval(Container.DataItem, "ItemID")  %&gt;' /&gt;
                                  &lt;/div&gt;
                              &lt;/td&gt;
  
                          &lt;/tr&gt;                 
                      &lt;/table&gt;                   
                  &lt;/ItemTemplate&gt;                   
  
              &lt;/asp:ListView&gt;
  </code></pre>
  
  <p>Code Behind:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
          {    
              if (Page.IsPostBack == false)
              {                  
                  BindData();
              }           
  
          }
  
   private void BindData()
          {
              MyDataContext data = new MyDataContext();
              var result = from itm in data.ItemLists
                           where itm.ItemID == iItemID
  
                           select new
                           {
                               itm.ItemID,
                               itm.Description,                            
                               FeatureList =  itm.Features
                           };
  
              lvParent.DataSource = result;
              lvParent.DataBind();
  
          }
  
   protected void lvChild_ItemEditing(object sender, ListViewEditEventArgs e)
          {
              ListView lvChild = sender as ListView;            
  
              lvChild.EditIndex = e.NewEditIndex;
  
              lvChild.DataBind();
  
          }
  </code></pre>
  
  <p>Edit:</p>
  
  <pre><code>protected void lvChild_ItemEditing(object sender, ListViewEditEventArgs e)
              {
                  ListView lvChild = sender as ListView;            
  
                  lvChild.EditIndex = e.NewEditIndex;
  
                  lvChild.DataBind();
  
              }
  </code></pre>
  
  <p>If I use "lvChild.DataBind()" in 'ItemEditing' event, the total list of child items goes away if I click 'edit'</p>
  
  <pre><code>protected void lvChild_ItemEditing(object sender, ListViewEditEventArgs e)
              {
                  ListView lvChild = sender as ListView;            
  
                  lvChild.EditIndex = e.NewEditIndex;              
  
  
              }
  </code></pre>
  
  <p>if I get rid of 'lvChild.Databind' in ItemEditing event, it goes to Edit mode after clicking the 'edit' button twice . And  though it shows textbox control of EditItemTemplate, it appears as a blank textbox (does not bind existing value to edit).</p>