ASP.NET Replicator (+ a question about best practices)

I am new to ASP.NET and I recently discovered repeaters. Some people use them, others do not, and I'm not sure which solution would be best practice.

From what I experienced, it simplifies a simple job (displays a list), but as soon as you want to do more complex things, complexity explodes, logically wise.

Maybe only I and I poorly understand the concept (this is very possible), so here is an example of what I'm trying to do, and my problem:


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

Decision

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


New problem . I want to be able to delete these files.

Solution . I add a delete link to the element template:

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

I will catch the event:

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

... then what? How to get the path to the file that I want to delete here, knowing that e.Item.DataItem is null (I started the debugger).

Did I just spend my time using repeaters when I could do the same, using a loop that would be just as simple, just-not quick elegant?

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

+4
source share
4 answers

You can definitely handle LinkButton events during the show. You can add CommandArgument to your LinkButton as follows:

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

Then in the code you can do this:

 string path = e.CommandArgument.ToString(); 

In general, I am a fan of repeater management. This gives you the ability to quickly do repetitive things, with limited code and a high level of control over the generated HTML. I prefer it over GridView and other more complex controls, because you have a much finer tweak to generate the output exactly as you need.

I prefer to loop it because I think you can grow faster with fewer errors if you don't add tons of HTML together in your code to create the generated HTML.

+9
source

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

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

+2
source

Repeaters are faster and more flexible than similar options because repeaters do not add their own code. To some extent they are glorified for the loop, although I think it is probably best to use a repeater, since it stores all your html codes in the same place.

+1
source

Rock repeaters. Especially repeaters of user controls that complete this function.

Anyway, the CommandArgument trick works very well, especially if you only need one argument. Another trick is to associate things with a separate handler (NOT the ItemCommand handler), and then use the sender to return to the item and get other data. i.e:

 <asp:LinkButton CommandArgument="<%# (string)Container.DataItem %>" ID="lbFileDelete" runat="server" Text="delete" OnClick="DeleteFile" /> <asp:Hidden runat="server" id="FileId" value="<%# DataBinder.Eval(Container.DataItem, "ID") %> 

And then in codebehind:

 protected void DeleteFile(object sender, EventArgs e) { LinkButton clicked = (LinkButton)sender; Control container = clicked.NamingContainer; int id = int.Parse(((Hidden)container.FindControl("FileId")).Value); //do stuff with the id, etc. } 

Quite convenient in more complex scenarios. Honestly, I never found anything in ASP.NET that cannot be done with a relay and a little ingenuity.

0
source

All Articles