【问题标题】:ASP.NET C# Continue row count in Gridview in RepeaterASP.NET C#在Repeater的Gridview中继续行计数
【发布时间】:2017-03-07 15:36:08
【问题描述】:

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

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

目前的结果:
Gridview1
1
2
3
4
5

Gridview2
1
2
3
4
5

Gridview3
1
2
3
4
5

期望的结果:
Gridview1
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();
}
}

【问题讨论】:

    标签: c# asp.net gridview repeater


    【解决方案1】:

    在事件处理程序之外定义计数器,一个好的位置是在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();
        }
    }
    

    【讨论】:

    • 我添加了我的代码。这只是我问过的第二个问题,所以不知道该怎么做。谢谢!
    • 计数器变量如何递增?
    • 对不起,我的错。现在它在那里。增量是在将文本添加到单元格之前,因为它最初是 0,您似乎想从 1 开始行计数。
    • 很高兴我能帮上忙。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多