在gridview分页时出现指定的参数已超出有效值的范围。 参数名: index,求打救
这是我的代码
前台:
<asp:GridView ID="GridView1" runat="server"
onrowcommand="GridView1_RowCommand" AutoGenerateColumns="False"
AllowSorting="True" BackColor="White" BorderColor="#CCCCCC"
BorderStyle="None" BorderWidth="1px"
CssClass="gridview_m" onrowdatabound="GridView1_RowDataBound"
Width="100%" AllowPaging="True"
onpageindexchanging="GridView1_PageIndexChanging" PageSize="4">
<Columns>
<asp:BoundField DataField="FirstProjectName" HeaderText="一级项目" />
<asp:BoundField DataField="SecondProjectName" HeaderText="二级项目" />
<asp:BoundField DataField="ThridProjectName" HeaderText="三级项目" />
<asp:BoundField DataField="recoupecontent" HeaderText="报销内容" />
<asp:BoundField DataField="recoupemoney" HeaderText="预报销金额(元)" />
<asp:BoundField DataField="recoupetime" HeaderText="报销时间" />
<asp:BoundField DataField="handingperson" HeaderText="经手人" />
<asp:TemplateField HeaderText="状态">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="pager" CommandArgument=<%#Eval("state")%> Text="未审核" Enabled="false"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle HorizontalAlign="Center" VerticalAlign="Middle" ForeColor="#000066" />
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle"
BackColor="#006699" Font-Bold="True" ForeColor="White" />
<%--执行分页--%>
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<PagerTemplate>
当前第:
<%--//((GridView)Container.NamingContainer)就是为了得到当前的控件--%>
<asp:Label ID="LabelCurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>
页/共:
<%-- //得到分页页面的总数--%>
<asp:Label ID="LabelPageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label>
页
<%-- //如果该分页是首分页,那么该连接就不会显示了.同时对应了自带识别的命令参数CommandArgument--%>
<asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
Visible='<%#((GridView)Container.NamingContainer).PageIndex != 0 %>'>首页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev"
CommandName="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'>上一页</asp:LinkButton>
<%--//如果该分页是尾页,那么该连接就不会显示了--%>
<asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"
Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>下一页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"
Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>尾页</asp:LinkButton>
转到第
<asp:TextBox ID="txtNewPageIndex" runat="server" Width="20px" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />页
<%--//这里将CommandArgument即使点击该按钮e.newIndex 值为3 --%>
<asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-2"
CommandName="Page" Text="GO" />
</PagerTemplate>
<PagerSettings Position="Bottom" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" Width="100%" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
后台:
//控制linkbutton的显示
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
LinkButton lbtn = (LinkButton)e.Row.Cells[7].FindControl("LinkButton1");
if (e.Row.RowType == DataControlRowType.DataRow)
{
string ID = lbtn.CommandArgument.ToString();
switch (ID)
{
case "0":
lbtn.Text = "未审核";
lbtn.Enabled = false;
break;
case "1":
lbtn.Text = "已审核";
lbtn.Enabled = false;
break;
case "2":
lbtn.Text = "查看修改情况";
lbtn.Enabled = true;
break;
}
}
}
//分页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView theGrid = sender as GridView;
int newPageIndex = 0;
if (e.NewPageIndex == -3)
{
TextBox txtNewPageIndex = null;
GridViewRow pagerRow = theGrid.BottomPagerRow;
if (pagerRow != null)
{
txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
}
if (txtNewPageIndex != null)
{
newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
}
}
else
{
newPageIndex = e.NewPageIndex;
}
newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
theGrid.PageIndex = newPageIndex;
pg_bind();
}
如果我去把girdview里的AllowPaging="True"改为false,就不会出现了