Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET repeater issue (+ question about best practices)
    text
    copied!<p>I am fairly new to ASP.NET and I discovered repeaters recently. Some people use them, other don't and I'm not sure which solution would be the best practice.</p> <p>From what I've experienced, it makes simple operation (display a list) simple, but as soon as you want to do more complicated things the complexity explodes, logic wise. </p> <p>Maybe it's just me and me poor understanding of the concept (this is highly possible), so here's an example of what am I trying to do and my issue:</p> <hr> <p><strong>Problem</strong>: I want to display a list of files located in a folder.</p> <p><strong>Solution</strong>:</p> <pre><code>String fileDirectory = Server.MapPath("/public/uploaded_files/"); String[] files = Directory.GetFiles(fileDirectory); repFiles.DataSource = files; repFiles.DataBind(); </code></pre> <p>and</p> <pre><code>&lt;asp:Repeater ID="repFiles" runat="server" OnItemCommand="repFiles_ItemCommand" &gt; &lt;ItemTemplate&gt; &lt;a href="/public/uploaded_files/&lt;%# System.IO.Path.GetFileName((string)Container.DataItem) %&gt;" target="_blank"&gt;View in a new window&lt;/a&gt; &lt;br /&gt; &lt;/ItemTemplate&gt; &lt;/asp:Repeater&gt; </code></pre> <p>This works fine.</p> <hr> <p><strong>New problem</strong>: I want to be able to delete those files.</p> <p><strong>Solution</strong>: I add a delete link in the item template:</p> <pre><code>&lt;asp:LinkButton ID="lbFileDelete" runat="server" Text="delete" CommandName="delete" /&gt; </code></pre> <p>I catch the event:</p> <pre><code> protected void repFiles_ItemCommand(object source, RepeaterCommandEventArgs e) { if (e.CommandName == "delete") { // ... blah } } </code></pre> <p>... then what? How do I get the file path that I want to remove from here knowing that e.Item.DataItem is null (I ran the debugger). </p> <p>Did I just wasted my time using repeaters when I could have done the same thing using an loop, which would have been just as simple, just -maybe- less elegant?</p> <p>What is the real advantage of using repeaters over other solutions?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload