简体   繁体   English

ASP.net ListView与组

[英]ASP.net ListView with groups

I can't seem to find a good answer for this. 我似乎找不到一个好的答案。 I'm trying to set up a webpage to grab data from a database and display it something like: 我正在尝试建立一个网页以从数据库中获取数据并显示如下内容:

username1 用户名1
image1.jpg image1.jpg
imagename 图片名称

image2.jpg image2.jpg
imagename2 imagename2

username2 用户名2
image3.jpg image3.jpg
imagename3 imagename3

username3 用户名3
image4.jpg image4.jpg
imagename4 imagename4

image5.jpg image5.jpg
imagename5 imagename5

image6.jpg image6.jpg
imagename6 imagename6

There is a username, and with that username there is one or more images and image names. 有一个用户名,使用该用户名有一个或多个图像和图像名称。 Not all the usernames will have the same number of images. 并非所有用户名都具有相同数量的图像。

My question is, what is the best way to do this? 我的问题是,最好的方法是什么? I am able to accomplish this using a repeater control with a nested repeater. 我可以使用带有嵌套转发器的转发器控件来完成此操作。 It's messy but works. 杂乱无章但可行。

From what I've read, a ListView should be able to do the same thing but much cleaner. 根据我的阅读,ListView应该能够执行相同的操作,但是更加简洁。 My problem with a ListView is that I have to set the GroupItemCount, it's not dynamic like i need. 我与ListView的问题是我必须设置GroupItemCount,它不是我需要的动态。 I think I can nest another ListView, but it turns out to be as much code as using a repeater. 我想我可以嵌套另一个ListView,但是事实证明,它和使用转发器一样多。

Is one of these methods (repeater or listview) preferred over the other. 是其中一种方法(中继器或列表视图)优于另一种方法。 Or is there a better way to do this that i'm not thinking of? 还是有我没有想到的更好的方法来做到这一点? I don't think that what i'm trying to do is out of the ordinary, so I think there would be a quicker way. 我不认为我要做的事情与众不同,所以我认为会有更快的方法。 To me, it seems much easier to do this in classic asp using for loops. 对我来说,使用for循环在经典asp中执行此操作似乎要容易得多。

Thanks in advance for any input. 预先感谢您的任何投入。

The GroupTemplate is instantiated when a certain number (GroupItemCount) of ItemTemplate are instantiated. 当实例化一定数量(GroupItemCount)的ItemTemplate时,实例化GroupTemplate。 The ItemTemplate is called for each object in the collection and you first have a collection of users. 将为集合中的每个对象调用ItemTemplate,您首先拥有一个用户集合。 Even if you can change the GroupItemCount during the ItemDataBound event, this will reflect how the next group of users is created. 即使您可以在ItemDataBound事件期间更改GroupItemCount,这也将反映如何创建下一组用户。 There is nothing about images until now, so I can say that you can't use ListView for a such task. 到目前为止,有关图像的信息还没有,所以我可以说您不能将ListView用于此类任务。

Using a repeater inside the ItemTemplate of the main Repeater (or ListView if you want) is the best choice, also common, so you don't have to worry about the mess, another developer will easily see your intention. 最好的选择是在主Repeater的ItemTemplate内部使用Repeater(如果需要,也可以使用ListView),这也是常见的选择,因此您不必担心混乱,另一个开发人员将很容易看到您的意图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM