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

标签 c# asp.net gridview repeater

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

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

当前结果:
GridView 1
1
2
3
4
5

Gridview2
1
2
3
4
5

Gridview3
1
2
3
4
5

期望的结果:
GridView 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)分部类之后...:

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();
    }
}

关于c# - ASP.NET C# 在 Repeater 的 Gridview 中继续行计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42652287/

相关文章:

php - : how to ajax update the cgridview yii

c# - VisualStudio Visual C# DataReceived 不规律地触发(从 Arduino 接收数据)

c# - Azure WebJob 和连接来自 Microsoft.Extensions.DependencyInjection 的 IServiceCollecton

c# - 没有标签的 HtmlGenericControl

asp.net - 如何在 ASP.NET MVC 应用程序中最好地实现和构造 View ,以便在页面加载时获得更好的缓存?

c# - 在编辑模式下无法从 GridView 获取复选框值

ios - iOS 中的 GridView

c# - 为什么 int.MaxValue - int.MinValue = -1?

c# - 如何使用 Microsoft.Graph 客户端库获取分配的许可证

c# - 在 aspx 页面中使用 Asp.Net WebForms 上的属性资源本地化文本