我有一个带有标签控件的列,该控件将计数保留在嵌套在中继器中的 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/