title: Hide deleted row, from DataList without rebinding the DataSource
body: <p>Please consider these operations -</p>
  
  <ol>
  <li>Bind a list of files in a <strong>DataList</strong>.</li>
  <li>Once the user clicks delete <strong>LinkButton</strong>, delete the file, in
  <strong>ItemCommandEvent</strong>.</li>
  <li>Since rebinding whole data is inefficient, I am simply hiding
  deleted row.</li>
  </ol>
  
  <p>Following code displays files in a DataList. There is a delete button beside each row.</p>
  
  <pre><code>&lt;asp:DataList ID="DataList1" OnItemCommand="DataList1_ItemCommand" runat="server"&gt;                
      &lt;ItemTemplate&gt;        
          &lt;tr&gt;
              &lt;asp:Label Text='&lt;%# Eval("ContainingFolder") as string + "\\" + Eval("FileName") as string %&gt;' 
                  Visible="false" ID="lblFullPath" runat="server" /&gt;
              &lt;td&gt;&lt;%# Eval("FileName") %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%# Eval("ContainingFolder") %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%# Eval("FileSize") %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%# Eval("Modified") %&gt;&lt;/td&gt;
              &lt;td&gt;
                  &lt;asp:LinkButton Text="Delete" ID="linkDelete" runat="server" /&gt;
              &lt;/td&gt;
          &lt;/tr&gt;            
      &lt;/ItemTemplate&gt;
  &lt;/asp:DataList&gt;
  </code></pre>
  
  <p>In ItemCommand Event Handler this code deletes the selected file from this list.</p>
  
  <pre><code>protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
  {
      if (e.Item.ItemType != ListItemType.Item &amp;&amp; e.Item.ItemType != ListItemType.AlternatingItem) return;
  
      var selectedItem = e.Item.FindControl("lblFullPath") as Label;
  
      e.Item.Visible = false;       //doesn't work     
  
      File.Delete(selectedItem.Text);
  }
  </code></pre>
  
  <p>However <strong>e.Item.Visible = false</strong> does not hides the row.
  As a workaround found here, <a href="https://stackoverflow.com/questions/9394887/how-to-hide-an-item-in-datalist">How to hide an item in datalist</a></p>
  
  <p>I have wrapped contents inside a placeholder control.</p>
  
  <pre><code>&lt;ItemTemplate&gt;
      &lt;asp:PlaceHolder ID="ph1" runat="server"&gt;
          &lt;%--wrapped content--%&gt;
      &lt;/asp:PlaceHolder&gt;
  &lt;/ItemTemplate&gt;
  </code></pre>
  
  <p>And hiding the placeholder control -</p>
  
  <pre><code>protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
  {
      if (e.Item.ItemType != ListItemType.Item &amp;&amp; e.Item.ItemType != ListItemType.AlternatingItem) return;
  
      var selectedItem = e.Item.FindControl("lblFullPath") as Label;
      //e.Item.Visible = false;
      e.Item.FindControl("ph1").Visible = false;
      File.Delete(selectedItem.Text);
  }
  </code></pre>
  
  <blockquote>
    <p>Generally in asp.net hiding parent control hides all of its child controls.</p>
  </blockquote>
  
  <p>But I am not able to understand, </p>
  
  <blockquote>
    <p>Why hiding the parent control e.Item doesn't hide its containing
    elements, in case of DataList ? Can you please advise. </p>
  </blockquote>
  
  <p>Thank you for your help.</p>
  
  <p><strong>EDIT:</strong> Updating more info about generating list of files to display in DataList.</p>
  
  <ul>
  <li>Generating fileList at runtime on button click, <strong>this list is not
  persistent, currently.</strong></li>
  </ul>
  
  <blockquote>
  <pre><code>protected void btnFindDuplicates_Click(object sender, EventArgs e)
  {
      DataList1.DataSource = FindDuplicates();
      DataList1.DataBind();
  }
  </code></pre>
  </blockquote>
  
  <ul>
  <li>In the event handler of delete Click, delete operation has been
  completed successfully.</li>
  <li>So, just to remove deleted item from the page, It would not be wise
  to regenerate the dataSource and bind this. Hiding seems more logical to me.</li>
  </ul>