ASP.NET C# 在 Repeater 的 Gridview 中继续行计数

ASP.NET C# Continue row count in Gridview in Repeater

我有一个带有 Label 控件的列,该控件将 Count 保存在嵌套在 Repeater 中的 Gridview 中。如果显示 3 个 Gridview,每个显示 5 行,如何在 Gridview 的 RowDataBound 事件中继续编号?目前,当我使用

时,Gridview 会重新开始
(e.Row.FindControl("lblCount") as Label).Text = (e.Row.RowIndex +1).ToString();

当前结果:
网格视图 1
1
2
3
4
5

Gridview2
1
2
3
4
5

Gridview3
1
2
3
4
5

期望的结果:
网格视图 1
1
2
3
4
5

Gridview2
6
7
8
9
10

Gridview3
11
12
13
14
15

.aspx 页面

    <asp:Repeater ID="rptGridViews" OnItemDataBound="rptGridViews_ItemDataBound" runat="server">
    <ItemTemplate>
        <asp:GridView ID="gvProposals" OnRowDataBound="gvProposals_RowDataBound" runat="server">
            <Columns>
                <asp:TemplateField HeaderText="Count">
                     <ItemTemplate>
                          <asp:Label ID="lblCount" runat="server" />
                     </ItemTemplate>
                </asp:TemplateField>
            </Columns>
         </asp:Gridview>
       </ItemTemplate>
    </asp:Repeater>

.aspx.cs

protected void gvProposals_RowDataBound(object sender, GridViewRowEventArgs e){
if(e.Row.RowType == DataControlRowType.DataRow)
{
  (e.Row.FindControl("lblCount") as Label).Text = (e.Row.RowIndex+1).ToString();
}
}

在事件处理程序之外定义计数器,一个好的地方是在 Page_Load 处理程序之前,就在您的 public partial class...:

之后
private int counter;

并且在 RowDataBound 事件处理程序中:

protected void gvProposals_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
        {
            counter++;
            (e.Row.FindControl("lblCount") as Label).Text = counter.ToString();
    }
}