简体   繁体   English

DataBind和/或asp:Repeater的迭代器整数

[英]Iterator integer for DataBind and/or asp:Repeater

I have a LINQ query that returns a custom object. 我有一个返回自定义对象的LINQ查询。 The object is derived from a Dictionary<T> of result objects, each with a URL. 该对象从结果对象的Dictionary<T>派生,每个结果对象都有一个URL。 My LINQ object groups the Dictionary<T> according to a URL property in T, and then takes the object.First() item from the Group of URLs. 我的LINQ对象根据Dictionary<T>中的URL属性对Dictionary<T>进行分组,然后从URL组中获取object.First()项。

Now I have a LINQ object all with unique URLs, and a count too of how many times the URL was repeated in the original Dictionary<T>. 现在,我有了一个全部带有唯一URL的LINQ对象,并且还计算了在原始Dictionary<T>. URL重复了多少次Dictionary<T>.

Ok, at this point I perform a DataBind on my LINQ object, to an ASP page. 好的,这时,我在LINQ对象上对ASP页面执行了DataBind。 The DataBind is to an <asp:Repeater> with an ItemTemplate . DataBind指向具有ItemTemplate<asp:Repeater>

What I want is to get the iterator of the <asp:Repeater> as if it is a simple for (int i = 0; ... i++) type loop. 我想要的是获取<asp:Repeater>的迭代器,就好像它for (int i = 0; ... i++)类型循环来说很简单。 This is so I can dynamically display the number of the result (say if there was a list of five results, each result would have 1. Result .... 2. Result .... 3. Result ... 这样一来,我就可以动态显示结果的数量(例如,如果有五个结果的列表,则每个结果将具有1.结果.... 2.结果.... 3.结果...

I do not know how many result will be there to start with (there could be none). 我不知道从那里开始会有多少结果(可能没有)。

How could this be achieved? 如何做到这一点? Could I use a lambda expression to alter the results object and hold an integer for its position in my Linq list?? 我可以使用lambda表达式来更改结果对象,并在Linq列表中为其位置保留一个整数吗?

I hope this isn't too confusing! 我希望这不会太令人困惑! Please ask if you need a better explanation. 请询问您是否需要更好的解释。 Much appreciated, as always. 一如既往地非常感谢。

If you want to display the row number in Repeater it is actually a lot easier than I think you realize. 如果要在Repeater显示行号,实际上比我想象的要容易得多。 You don't need to know the current index since you can count / increment a value everytime a bind occurs and display it. 您不需要知道当前索引,因为每次绑定发生并显示它时,您都可以计数/增加一个值。

First create a integer in your .cs code. 首先在.cs代码中创建一个整数。 Eg: 例如:

public partial class _YourPageName : System.Web.UI.Page
{
    private int _itemCounter = 0;

Then in your Repeater ItemTemplate you just need to display it using a Literal or something. 然后,在Repeater ItemTemplate您只需要使用Literal或其他内容显示它。

<asp:Literal ID="litCount" runat="server" OnDataBinding="litCount_DataBinding" />

Then implement it the DataBinding event for the Literal or whatever you want to use: 然后为Literal或您要使用的任何对象实现DataBinding事件:

protected void litCount_DataBinding(object sender, System.EventArgs e)
{
    _itemCounter++;
    Literal lt = (Literal)(sender);
    lt.Text = _itemCounter.ToString();
}

Each time the ItemTemplate is bound it will increment the counter and display it. 每次绑定ItemTemplate ,它将增加计数器并显示它。

Maybe I am over simplifying your issue though. 也许我不过是简化了您的问题。

It's not entirely clear what you're doing, particularly as you've referred to Dictionary as if it had one type parameter instead of two, but you might want to consider this: 这并不完全清楚自己在做什么,特别是你提到Dictionary ,好像有一种类型的参数,而不是两个,但你可能要考虑这个问题:

var items = originalItems.Select((Value, Index) => new { Value, Index });

That overload of Select provides the index for each entry as it iterators - and I've just captured the results in an anonymous type. Select重载为迭代器的每个条目提供了索引-我刚刚以匿名类型捕获了结果。 (You might want to use a Tuple<,> instead if you're using .NET 4; it depends on where the results will be used.) (如果您使用的是.NET 4,则可能要使用Tuple<,> ;它取决于将结果用于何处。)

This will start at Index=0 of course - to start from 1 you'd need: 当然,这将从索引= 0开始-从1开始,您需要:

var items = originalItems.Select((Value, idx) => new { Value, Index = idx + 1 });

(Parameter renamed to keep it on one short line for Stack Overflow.) (已重命名参数,以使其在堆栈溢出中仅停留一小段。)

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

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