为什么我的datagrid.pagecount总是0,我快疯了,整整弄了三天分页

netfirst 2007-04-19 02:33:57
//计算页数
public int pcount(DataGrid MyDataGrid)
{
return MyDataGrid.PageCount;
}
//响应分页事件
public void pagechange(string pagemark,DataGrid MyDataGrid)
{
int pcount=this.pcount(MyDataGrid);
switch(pagemark)
{
case "first":
MyDataGrid.CurrentPageIndex=0;
break;
case "next":
if(MyDataGrid.CurrentPageIndex<pcount-1) MyDataGrid.CurrentPageIndex+=1; break;
case "pre":
if(MyDataGrid.CurrentPageIndex>0)
MyDataGrid.CurrentPageIndex-=1;
break;
case "last":
MyDataGrid.CurrentPageIndex = pcount - 1;
break;
}
}
//是不是点击超连接<a href="">(runat不是server)不触发IsPostBack事件????????
if(!this.Page.IsPostBack)
{
this.toptitle.Text="<div align=center><b>查询结果如下:</b></div>";
this.filldate(gj,lx);//将datareader中的内容放入会话表格中,既this.Session["searchtable"]);
this.DataGrid2.VirtualItemCount=((DataTable)this.Session["searchtable"]).Rows.Count;
//用于检测是first,next,pre,last,以便触发上一页,下一页,最后页,首页
if(this.Request.QueryString["type"]!=null)
{
string type=this.Request.QueryString["type"].ToString();
this.pagechange(type,this.DataGrid2);
this.Response.Write(this.DataGrid2.CurrentPageIndex);//测试用,但每次都是0
startIndex = this.DataGrid2.CurrentPageIndex * this.DataGrid2.PageSize;
this.Response.Write(this.DataGrid2.PageCount);//测试用,但每次都是0
this.BindGrid();
}
else
{
this.BindGrid();
}
}
//OnPageIndexChanged="mydatechange"(html中触发DataGrid2分页),该段代码实现了
protected void mydatechange(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid2.CurrentPageIndex = e.NewPageIndex;
startIndex = this.DataGrid2.CurrentPageIndex * this.DataGrid2.PageSize;
BindGrid();
}
private void BindGrid()
{
this.DataGrid2.DataSource = CreateDataSource();
this.DataGrid2.DataBind();
}
//其中DataGrid2自带的页码实现了,但就是上页下页,首页,末页总也出现currentpageindex=0,pagecount=0我也不知道是为什么?????????
...全文
307 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ycqing 2007-04-20
  • 打赏
  • 举报
回复
public void pagechange(string pagemark,DataGrid MyDataGrid)
{
int pcount=this.pcount(MyDataGrid);
switch(pagemark)
{
case "first":
MyDataGrid.CurrentPageIndex=0;
break;
case "next":
if(MyDataGrid.CurrentPageIndex<pcount-1)MyDataGrid.CurrentPageIndex+=1;break;
case "pre":
if(MyDataGrid.CurrentPageIndex>0)
MyDataGrid.CurrentPageIndex-=1;
break;
case "last":
MyDataGrid.CurrentPageIndex = pcount - 1;
break;
}
}
你的 这些可以 执行到?? 设断点检查下,参考下网上的 例子吧
netfirst 2007-04-20
  • 打赏
  • 举报
回复
ycqing() 我试过,可以 执行到!!!!!
我开始使用下面的函数计算页总数的!!!!!!代码如下:
//计算页数,这也是由于datagrid.pagecount总是0出现逼的我写了一个函数///
public int pcount(int count,int pagesize)
{
int pcount=0;
if(count%pagesize==0)
pcount=count/pagesize;
else
pcount=count/pagesize+1;
return pcount;
}
//这个函数就可以记录实际的页数!!没问题!!!!!!!!
当点"第一页"和"最后页"时没问题,但每点击"上一页"和"下一页"就会出现错误!!!!!!!!!!
因为点击"下一页"和"上一页"都会用到MyDataGrid.CurrentPageIndex(每次都是0)
而点击"第一页"和"最后页"不用MyDataGrid.CurrentPageIndex计数,而是直接赋植的
netfirst 2007-04-19
  • 打赏
  • 举报
回复
//这是html中的DataGrid2
<asp:datagrid id="DataGrid2" runat="server" Width="560px" AutoGenerateColumns="False" ShowHeader="False"
GridLines="None" AllowCustomPaging="True" PageSize="6" AllowPaging="True" OnPageIndexChanged="mydatechange">
//另外这是根据当前页码更换显示状态,即可连接,不可连接
public string ShowPagefoot(DataGrid MyDataGrid,string http,int notecount)
{
int pcount=Convert.ToInt32(MyDataGrid.PageCount.ToString());
int pageNO=MyDataGrid.CurrentPageIndex+1;
string pagefoot="";
pagefoot="<font color=darkgray size=2>总计"+notecount+"条</font> ";
if(pcount==1)
{
pagefoot+="<font color=darkgray size=2 class=bt>第一页 上一页 下一页 最后页</font>";
}
else
{
if(MyDataGrid.CurrentPageIndex==0)
pagefoot+="<font color=darkgray size=2 class=bt>第一页 上一页</font> <a class=bt id=next href="+http+"&type=next>下一页</a> <a class=bt id=last href="+http+"&type=last>最后页</a>";
else
{
if(MyDataGrid.CurrentPageIndex==pcount-1)
pagefoot+="<a class=bt id=first href="+http+"&type=first>第一页</a> <a class=bt id=pre href="+http+"&type=pre>上一页</a> <font color=darkgray size=2 class=bt>下一页 最后页";
else
pagefoot+="<a class=bt id=first href="+http+"&type=first>第一页</a> <a class=bt id=pre href="+http+"&type=pre>上一页</a> <a class=bt id=next href="+http+"&type=next>下一页</a> <a class=bt id=last href="+http+"&type=last>最后页</a>";
}
}
pagefoot+=" 第"+pageNO+"页 共"+pcount+"页  </font><input type=text id=pnum size=10><input type=submit id=tj value=go>";
return pagefoot;
}

JN365 2007-04-19
  • 打赏
  • 举报
回复
热烈祝贺ASP.NET群7947148成立了。
netfirst 2007-04-19
  • 打赏
  • 举报
回复
还是不行!!真是谢谢你们了
netfirst 2007-04-19
  • 打赏
  • 举报
回复
ztwz(-_-b(偶要像海绵一样的狂吸水水!)) 你说的是不是在page_load事件里放下面的这些!
if(!this.Page.IsPostBack)
{
if(this.Request.QueryString["type"]!=null)
{
string type=this.Request.QueryString["type"].ToString();
this.pagechange(type,this.DataGrid2);
this.Response.Write(this.DataGrid2.CurrentPageIndex);//测试用,但每次都是0
startIndex = this.DataGrid2.CurrentPageIndex * this.DataGrid2.PageSize;
this.Response.Write(this.DataGrid2.PageCount);//测试用,但每次都是0
this.BindGrid();
}
.............
我放了,但我测试了:::点击超连接<a href="">(runat不是server)不触发IsPostBack事件!
scow 2007-04-19
  • 打赏
  • 举报
回复
datagrid.AllowPaging
ztwz 2007-04-19
  • 打赏
  • 举报
回复
是不是page_load事件里的bind方法没放在IsPostBack中的问题?
netfirst 2007-04-19
  • 打赏
  • 举报
回复
那怎么放呢
极客行天下 2007-04-19
  • 打赏
  • 举报
回复
应该放在数据绑定动作之后才行吧
netfirst 2007-04-19
  • 打赏
  • 举报
回复
有记录!!!!!!!
绝代坏坏 2007-04-19
  • 打赏
  • 举报
回复
查一下你的sql语句吧。有没有记录。

62,046

社区成员

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

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

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

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