title: ListView DataItem Shows Null
body: <p>A few days ago, I <a href="https://stackoverflow.com/questions/583689/dictionaryt-of-listt-and-listviews-in-asp-net">wrote about issues</a> with implementing a ListView in ASP.NET.  Now, with all of the other code written, I'm having trouble saving changed items in a ListView.</p>
  <p>A few things of note:</p>
  <ul>
  <li>The Save button is not part of the ListView proper; it calls the <code>GetListViewItems()</code> method, which in turns call the <code>Save()</code> method.</li>
  <li>The <code>Listview.DataBind()</code> event is invoked when a button is pressed requesting the records to be updated</li>
  <li>The Listview shows text using <code>&lt;%#Eval(&quot;Key.Name&quot;) %&gt;</code> and a <a href="https://stackoverflow.com/questions/595761/concatenating-two-properties-for-display-in-a-dropdownlist">named <code>DropDownList</code></a> using <code>&lt;%#Eval(&quot;Value&quot;) %&gt;</code></li>
  </ul>
  <h3>Getting The Items From the ListView</h3>
  <pre><code>public void GetListViewItems()
  {
   List&lt;Foo&gt; Result = FooManager.CreateFooList();
   DropDownList ddl = null;
   ListViewItem Item = null;
      try
        {
         foreach (ListViewDataItem item in lvFooList.Items)
           {
            Item = item;
            ddl = ((DropDownList) (Item.FindControl(&quot;ddlListOfBars&quot;)));
            if (//something is there)
             {
              Foo foo = FooManager.CreateFoo();
              foo.Id = item.DataItemIndex; //shows null
              int barId = int.Parse(ddl.SelectedItem.Value); //works just fine
              foo.barId = barId;
              Result.Add(foo);
             }
           }
        }
     catch (Exception ex)
       {
               //Irrelevant for our purposes
       }
  }
  </code></pre>
  <h3>DataBinding the ListView</h3>
  <p>The code to databind the ListView is <a href="https://stackoverflow.com/questions/583689/dictionaryt-of-listt-and-listviews-in-asp-net">shown here in my previous question</a>.</p>
  <h3>Question(s):</h3>
  <ol>
  <li>Why is it that when I iterate through the <code>ListViewDataItem</code> in the <code>Listview</code> that each item is <code>null</code>?</li>
  <li>How can I retrieve the <code>Foo.Id</code> from the Dictionary?</li>
  <li>What else might I be missing?</li>
  <li>What would I use if I wanted to get that <code>Id</code> Programmatically based on what items were shown?  As it is now, the current ListView is shown based on what <code>Foo</code>s were selected.  Those <code>Foo</code>s selected are then displayed, and the user can change the <code>Bar</code> in the <code>DropDownList</code>, hit Save, and those changes are propogated.</li>
  </ol>
  <hr />
  <h3>Update</h3>
  <p>As it turns out, my problem was what <a href="https://stackoverflow.com/questions/609276/listview-dataitem-shows-null/609331#609331">leppie</a> had said; and that was that I needed to specify <code>DataKeyNames</code> and use those to retain the information from the ListView.</p>
  <p>Here's the code I added:</p>
  <pre><code>try
  {
     int DataKeyArrayIndex = 0;
     foreach (ListViewDataItem item in lvFooList.Items)
       {
        Item = item;
        ddl = ((DropDownList) (Item.FindControl(&quot;ddlListOfBars&quot;)));
        if (//something is there)
         {
          Foo foo = FooManager.CreateFoo();
          Foo tempFoo = FooManager.CreateFoo();
          if (lvFooList != null)
          {
               tempFoo = ((Foo)(lvFooList.DataKeys[DataKeyArrayIndex].Value));
          }
           
          foo.Id = tempFoo.Id;
          int barId = int.Parse(ddl.SelectedItem.Value); //works just fine
          foo.barId = barId;
          Result.Add(foo);
          DataKeyArrayIndex++;
       }
     }
  }
  </code></pre>
  <p>And then in the <code>.ascx</code> file, I added <code>DataKeyNames=&quot;Key&quot;</code>, like so:</p>
  <pre><code>&lt;asp:ListView ID=&quot;lvFooList&quot; runat=&quot;server&quot; DataKeyNames=&quot;Key&quot;&gt;
  </code></pre>
  <p>This allowed me to use the <code>Key</code> <a href="https://stackoverflow.com/questions/583689/dictionaryt-of-listt-and-listviews-in-asp-net">from my previous post</a> to determine which Foo was being looked at.</p>
  <p>Any critiques of this approach, as well as methods for making it better are greatly appreciated.</p>