Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET repeater issue (+ question about best practices)

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.

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.

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:


Problem: I want to display a list of files located in a folder.

Solution:

String fileDirectory = Server.MapPath("/public/uploaded_files/");
String[] files = Directory.GetFiles(fileDirectory);
repFiles.DataSource = files;
repFiles.DataBind();

and

<asp:Repeater ID="repFiles" runat="server" OnItemCommand="repFiles_ItemCommand" >
        <ItemTemplate>
           <a href="/public/uploaded_files/<%# System.IO.Path.GetFileName((string)Container.DataItem) %>" target="_blank">View in a new window</a> 
           <br />
        </ItemTemplate>  
</asp:Repeater>

This works fine.


New problem: I want to be able to delete those files.

Solution: I add a delete link in the item template:

<asp:LinkButton ID="lbFileDelete" runat="server" Text="delete" CommandName="delete" />

I catch the event:

   protected void repFiles_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                // ... blah
            }
        }

... 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).

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?

What is the real advantage of using repeaters over other solutions?

like image 971
marcgg Avatar asked Dec 18 '22 07:12

marcgg


2 Answers

You can definitely handle the LinkButton events as you are showing. You can add a CommandArgument to your LinkButton like this:

<asp:LinkButton CommandArgument="<%# (string)Container.DataItem %>" ID="lbFileDelete" runat="server" Text="delete" CommandName="delete" />

Then in your code you can do this:

string path = e.CommandArgument.ToString();

In general, I'm a fan of the Repeater control. It gives you the ability to make repeating things quickly, with limited code and a high level of control over the generated HTML. I prefer it over the GridView and other more complex controls because you have much more fine-tuned ability to generate the output exactly as you need.

I prefer it over looping, because I believe you can develop faster, with fewer errors if you're not appending tons of HTML together in your code to make the generated HTML.

like image 85
JerSchneid Avatar answered Jan 07 '23 11:01

JerSchneid


For displaying a list of things, Repeaters are typically faster than GridViews, DataLists, and their other counterparts. Repeaters are best suited for display, rather than adding and editing records, although you can manually wire up what is needed to use a Repeater for CRUD operations.

In your example, you need to bind the file path to the CommandArgument property of your link button. Then you should have access to the path using e.CommandArgument in the event handler.

like image 42
JasonS Avatar answered Jan 07 '23 11:01

JasonS