在gridview分页时出现指定的参数已超出有效值的范围。 参数名: index,求打救

u010686548 2014-07-16 08:09:23
这是我的代码
前台:
<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,就不会出现了
...全文
267 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
万玩完顽完 2014-07-18
  • 打赏
  • 举报
回复
引用 6 楼 u010686548 的回复:
[quote=引用 4 楼 fangzuli 的回复:] if (txtNewPageIndex != null) { newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; } 这里获取到值了?你设断点看一下。 建议对newPageIndex TryParse 一下。
但是在其他页面可以使用,就是这个页面不行[/quote] 自己多对比一下吧。技术都差不多,看哪里不一样试着调试一下,心细点会出来的。
u010686548 2014-07-17
  • 打赏
  • 举报
回复
引用 4 楼 fangzuli 的回复:
if (txtNewPageIndex != null) { newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; } 这里获取到值了?你设断点看一下。 建议对newPageIndex TryParse 一下。
但是在其他页面可以使用,就是这个页面不行
u010686548 2014-07-17
  • 打赏
  • 举报
回复
引用 3 楼 5653325 的回复:
执行到哪一句报错?
假如我知道就不用问了。。。
万玩完顽完 2014-07-17
  • 打赏
  • 举报
回复
if (txtNewPageIndex != null) { newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; } 这里获取到值了?你设断点看一下。 建议对newPageIndex TryParse 一下。
踏平扶桑 2014-07-17
  • 打赏
  • 举报
回复
执行到哪一句报错?
u010686548 2014-07-16
  • 打赏
  • 举报
回复
引用 1 楼 wangnaisheng 的回复:
http://technet.microsoft.com/zh-cn/magazine/system.web.ui.webcontrols.gridview.allowpaging(VS.100).aspx 若要启用分页功能,请将 AllowPaging 属性设置为 true。
请看清楚我的问题
wangnaisheng 2014-07-16
  • 打赏
  • 举报
回复
http://technet.microsoft.com/zh-cn/magazine/system.web.ui.webcontrols.gridview.allowpaging(VS.100).aspx 若要启用分页功能,请将 AllowPaging 属性设置为 true。

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧