我有一个带有Label控件的列,它将Count保存在嵌套在Repeater中的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 page
<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();
}
}
答案 0 :(得分:2)
在事件处理程序之外定义计数器,在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();
}
}