问题描述
我有一个如下列表视图:
I have a list view like below:
<asp:ListView ID="lstTopRanks" runat="server">
<ItemTemplate>
<div class="Amazing-{recordNumber}">{itemdata}</div>
</ItemTemplate>
</asp:ListView>
我想用运行中的计数器替换 {recordNumber}
,这样显示的第一条记录为1,第二条记录为2,依此类推.
I would like to replace {recordNumber}
with a running counter so the first record shown have 1 the second will be 2 and so on.
我该怎么做?
预先感谢
推荐答案
如果ID不是,则可以使用 Container.DisplayIndex
或 Container.DataItemIndex
来自例如数据库.
You can do it with Container.DisplayIndex
, or Container.DataItemIndex
, if the ID is not coming from for example a database.
关键是这样: class ="Amazing-<%#Container.DisplayIndex + 1%>"
.
如果 recordNumber
确实来自外部数据源,则您也可以像这样进行操作: class ='Amazing-<%#Eval("YourDatabaseIDColumn")%>'
.
If recordNumber
does come from an external datasource, you can do that as well like this: class='Amazing-<%# Eval("YourDatabaseIDColumn") %>'
.
DisplayIndex和DataItemIndex不同.DisplayIndex是始终从当前页面开始的运行索引,而DataItemIndex是整个数据源中的运行编号.
DisplayIndex and DataItemIndex differ if you are paging your datasource. DisplayIndex is a running index always starting on the current page, and DataItemIndex is a running number in your whole datasource.
这里是一个例子:
//just to represent something like a db table with an ID and a description
Pair[] data = new Pair[] { new Pair(123, "row1"), new Pair(124, "row2"), new Pair(125, "row3"), new Pair(126, "row4"), new Pair(127, "row5"), new Pair(128, "row6"), new Pair(129, "row7"), new Pair(130, "row8"), new Pair(131, "row9"), new Pair(132, "row10") };
lstTopRanks.DataSource = data;
lstTopRanks.DataBind();
ASPX:
<asp:ListView ID="lstTopRanks" runat="server">
<LayoutTemplate><asp:PlaceHolder ID="ItemPlaceHolder" runat="server"></asp:PlaceHolder></LayoutTemplate>
<ItemTemplate>
<div class="Amazing-<%#Container.DisplayIndex + 1 %>">
DisplayIndex: <b><%#Container.DisplayIndex %></b>;
DataItemIndex: <b><%#Container.DataItemIndex %></b>,
ID and Text: <i><%#((Pair)Container.DataItem).First %>, <%#((Pair)Container.DataItem).Second %></i>
</div>
</ItemTemplate>
</asp:ListView>
<asp:DataPager ID="DataPagerProducts" runat="server" PagedControlID="lstTopRanks" PageSize="3" >
<Fields>
<asp:NextPreviousPagerField ShowFirstPageButton="True" ShowNextPageButton="False" />
<asp:NumericPagerField />
<asp:NextPreviousPagerField ShowLastPageButton="True" ShowPreviousPageButton="False" />
</Fields>
</asp:DataPager>
结果:
这篇关于如何在渲染时间中获取C#ASP.NET ListView中当前记录的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!