ASP.net Repeater — setting N Maximum Repetitions?

拟墨画扇 提交于 2019-12-11 08:56:07

问题


I am using a Repeater control to display data from a SQL server on multiple different sites right now. I am wondering if there is any way to make a repeater only "repeat" or run through its cycles a maximum of N times.

I split a Div element up into 4 equal sections which have just enough room for 4 cycles of this repeater. If it runs more than 4 times, it pushes everything down in the div and begins a new row of images/text I would prefer to avoid. It makes the web page look uglier and does not really accomplish much for the client.

Also is there any way to randomly select which iterations through the repeater get displayed? For example, one time the page loads, the repeater pulls iterations 2-5, then you refresh the page and the repeater pulls iterations 1-4. That would be very cool if I could make the images/text that show up to be random.

For those that are curious, a repeater is an ASP.net object which basically iteratively displays or otherwise works with data from a table, SQL database, etc. It just goes row by row through whatever the data is.


回答1:


Rather than try to do anything funky with the repeater, you can do this to your data source itself before binding it to your repeater.

For example:

var rand = new Random();
var myCollection = GetSomethingFromSomewhere();
myRepeater.DataSource = myCollection
    .OrderBy(x => rand.Next())
    .Take(4);
myRepeater.DataBind();


来源:https://stackoverflow.com/questions/17955743/asp-net-repeater-setting-n-maximum-repetitions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!