Accessing Data Items on Select with ListView
<p>Writing an asp.net file manager.</p>

<p>Essentially, the user clicks on a folder in a TreeView control and the files within that folder are shown in a ListView control.</p>

<p><strong>The ListView</strong></p>

<pre><code>&lt;asp:listview id="lvFiles" runat="server" onitemdeleting="lvFiles_ItemDeleting" 
      onselectedindexchanging="lvFiles_SelectedIndexChanging"&gt;
     &lt;layouttemplate&gt;
          &lt;table cellpadding="2" width="520px" border="1" id="tbl1" runat="server"&gt;
              &lt;tr id="Tr1" runat="server" style="background-color: #98FB98"&gt;
                  &lt;th id="Th0" runat="server"&gt;&lt;/th&gt;
                      &lt;th id="Th1" runat="server"&gt;Filename&lt;/th&gt;
                      &lt;th id="Th2" runat="server"&gt;Uploaded&lt;/th&gt;
                      &lt;th id="Th3" runat="server"&gt;Last Accessed&lt;/th&gt;
              &lt;/tr&gt;
              &lt;tr runat="server" id="itemPlaceholder" /&gt;
           &lt;/table&gt;
           &lt;asp:datapager id="DataPager1" runat="server" pagesize="25"&gt;
                &lt;fields&gt;
                    &lt;asp:nextpreviouspagerfield buttontype="Button" /&gt;
                &lt;/fields&gt;
           &lt;/asp:datapager&gt;
      &lt;/layouttemplate&gt;
      &lt;emptyitemtemplate&gt;
          &lt;p&gt;No items&lt;/p&gt;
      &lt;/emptyitemtemplate&gt;
      &lt;itemtemplate&gt;
          &lt;tr runat="server"&gt;
              &lt;td&gt;&lt;asp:linkbutton id="itemSelected" runat="server" tooltip='&lt;%# Eval("FullName") %&gt;' autopostback="True" commandname="select" text="Select" /&gt;
              &lt;/td&gt;
              &lt;td&gt;&lt;asp:label id="fNameLabel" runat="server" text='&lt;%# Eval("Name") %&gt;'&gt;&lt;/asp:label&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
       &lt;/itemtemplate&gt;
       &lt;selecteditemtemplate&gt;
           &lt;tr id="Tr2" runat="server"&gt;
               &lt;td&gt;Selected&lt;/td&gt;
               &lt;td&gt;&lt;asp:label id="fNameLabel" runat="server" text='&lt;%# Eval("Name") %&gt;'&gt;&lt;/asp:label&gt;
               &lt;/td&gt;
               &lt;td&gt;&lt;asp:button id="btnDelete" runat="server" text="Delete" commandname="Delete"&gt;&lt;/asp:button&gt;
               &lt;/td&gt;
           &lt;/tr&gt;
      &lt;/selecteditemtemplate&gt;
&lt;/asp:listview&gt;
</code></pre>

<p><strong>Binding the File List</strong></p>

<p>So currently what happens, is in the <code>TreeView_SelectedNodeChanged</code> event, the application takes the <code>DirectoryInfo</code> object represented by the <code>TreeNode</code> and gets an array of <code>FileInfo</code> objects, using the <code>DirectoryInfo.GetFiles()</code> method.</p>

<p>That <code>FileInfo[]</code> is passed to the following method.</p>

<pre><code>protected void AddFilesToViewPort(FileInfo[] Files)
{
    List&lt;FileInfo&gt; fList = new List&lt;FileInfo&gt;();
    for (int i = 0; i &lt; Files.Length; i++)
    {
        fList.Add(Files[i]);
    }
    lvFiles.DataSource = fList;
    lvFiles.DataBind();
    upExistingFiles.Update();
}
</code></pre>

<p>Which binds the <code>FileInfo[]</code> to the <code>ListView</code> object, <code>lvFiles</code>, which is pretty much exactly how I want it.</p>

<p>What I want to do is be able to select an item in the ListView (which can be done at the moment) then when the user presses the <code>Delete</code> button, I want the application to work with the file in question. Essentially, I want to move the file to a "Deleted Files" directory and log the action in the database.</p>

<p>The problem I'm having is getting the actual <code>FileInfo</code> object associated with the list item I select.</p>

<p>If I attach the debugger to the and step through, the <code>lvFiles_ItemDeleting</code> event is firing and I'm getting the index of the selected <code>ListItem</code> as I should be, but when I go through the objects in the debugger, the actual information about the object the <code>ListItem</code> is representing just isn't there.</p>

<p><img src="http://i.stack.imgur.com/SwtX8.png" alt="Image showing lack of DataKeys in the debugged ListView"></p>

<p>As you can see in the image above, the DataKeys property of the <code>ListView</code> is holding some information about its items, but when I dig deeper into that property, the information just isn't there.</p>

<p>How do I get the <code>FileInfo</code> object from the selected <code>ListViewItem</code>?</p>
