分页无法正常显示,求救现场高手!

explorerat2003 2004-03-20 09:55:50
我用DataGrid来显示数据库new.mdb中的aa表内容,并要进行分页,假如有记录7条,每页显示3条,分页按钮如下:
首页 上一页 下一页 尾页 页2/3 共7 条 3条记录/页
但这些按钮不能正常操作,选首页和尾页都可正常显示。不正常:(1)若当前页是第1页,选下一页可正常显示,即页2/3,但现在按下一页则内容和页2/3都没改变。(2)选尾页,现在显示页3/3,现在若选上一页,则会跳到第1页,而不是跳到第2页。
这个问题迷惑了我很久,到底错在哪里,恳请高手费心看一下,非常感激!!!(源代码new.aspx和new.cs如下:)
new.aspx:
<%@ Page Inherits="www.MyCodeBehind" Src="new.cs" %>
<html>
<head>
<title>新闻网站自动发布</title>
<body>
<form runat="server">
<table width="90%" border="0" align="center">
<tr>
<td width="608">
<div align="center"> <ASP:DataGrid id="MyList"
AllowPaging="True"
PageSize="3"
OnPageIndexChanged="MyList_Page"
EnableViewState="false"
AutoGenerateColumns="false"
DataKeyField="id"
OnDeleteCommand="MyDataGrid_Delete"
> </asp:DataGrid> </div>
</td>
</tr>
<tr>
<td width="53" height="11">
<div align="center"><asp:LinkButton id="btnFirst" runat="server"
Text="首 页"
CommandName="Pager"
CommandArgument="First"
OnCommand="PagerButtonClick"
/></div>
</td>
<td width="53" height="11">
<div align="center"> <asp:LinkButton id="btnPrev" runat="server"
Text="上一页"
CommandName="Pager"
CommandArgument="Prev"
OnCommand="PagerButtonClick"
/></div>
</td>
<td width="53" height="11">
<div align="center"><asp:LinkButton id="btnNext" runat="server"
Text="下一页"
CommandName="Pager"
CommandArgument="Next"
OnCommand="PagerButtonClick"
/></div>
</td>
<td width="53" height="11">
<div align="center"> <asp:LinkButton id="btnLast" runat="server"
Text="尾 页"
CommandName="Pager"
CommandArgument="Last"
OnCommand="PagerButtonClick"
/></div>
</td>
<td width="54" height="11">
<div align="center">页<%#CurrentPage.ToString()%> / <%#PageCount.ToString()%>
</font></div>
</td>
<td width="73" height="11">
<div align="center">共<%# RecordCount.ToString()%> 条</div>
</td>
<td width="72" height="11">
<div align="center"><%# MyList.PageSize.ToString()%>条记录/页</div>
</td>

</tr>

</table>
</form>
</body>
</html>
new.cs:
namespace www
{
public class MyCodeBehind : Page
{
public DataGrid MyList;
public LinkButton btnFirst;
public LinkButton btnPrev;
public LinkButton btnNext;
public LinkButton btnLast;
public int PageCount,RecordCount,CurrentPage;
//PageCount为总页数,RecordCount为总记录数,CurrentPage为当前页
private void Page_Load(Object sender, EventArgs e)
{ DataBind1();
}
DataView CreateDataSource()
{
OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+ Server.MapPath("new.mdb"));
OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from aa ", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "tt");
return ds.Tables["tt"].DefaultView;
}

void DataBind1()
{
DataView source=CreateDataSource();
RecordCount=source.Count;
PageCount=RecordCount/MyList.PageSize;
if((RecordCount%MyList.PageSize)!=0) PageCount++;
CurrentPage=MyList.CurrentPageIndex+1;
DataBind();
MyList.DataSource = source;
MyList.DataBind();

}

public void MyList_Page(Object sender, DataGridPageChangedEventArgs e)
{
MyList.CurrentPageIndex = e.NewPageIndex;
DataBind1();
}
public void PagerButtonClick(Object sender, CommandEventArgs e)
{
btnFirst.Enabled=true;
btnPrev.Enabled=true;
btnNext.Enabled=true;
btnLast.Enabled=true;
//由外部分页 UI 使用
String arg = e.CommandArgument.ToString();
switch(arg)
{
case "Next":
if (CurrentPage<PageCount)
CurrentPage++;
break;
case "Prev":
if (CurrentPage>1)
CurrentPage--;
break;
case "Last":
CurrentPage= PageCount;
break;
case "First":
CurrentPage=1;
break;
}
if(CurrentPage==1)
{
btnFirst.Enabled=false;
btnPrev.Enabled=false;
}
else if(CurrentPage==PageCount)
{
btnLast.Enabled=false;
btnNext.Enabled=false;
}
MyList.CurrentPageIndex=(CurrentPage-1);
DataBind1();
}
}
}
...全文
90 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
aquariusBoy 2004-03-20
  • 打赏
  • 举报
回复
解决方法就是每次load的时候,获得当前的页面号。

可以用Request.QueryString["pageindex"]获得点击前的页面值

也可以用一个hidden控件保存值
aquariusBoy 2004-03-20
  • 打赏
  • 举报
回复
你的PagerButtonClick里面应该首先获得当前页的值

因为每次你点了下一页后,page重新load就会把你的CurrentPage置为初始的值,你在pageload里面有处理它,所以每次都是1,到PagerButtonClick函数里面就变成2(CurrentPage++)

所以你点下一页就会一直保留在第二页。

尾页的错误和上面一样。

aquariusBoy 2004-03-20
  • 打赏
  • 举报
回复
断点跟踪看看,你的PagerButtonClick应该不会调用。
xz2000 2004-03-20
  • 打赏
  • 举报
回复
1.private static int CurrentPage;
用viewstate也可以
2.private int CurrentPage
{
set{ViewState["currentpage"]=value;}
get{ return Convert.ToInt32(ViewState["currentpage"]);}
}
jpyc 2004-03-20
  • 打赏
  • 举报
回复
http://218.56.11.178:8018/FileDown.aspx?FID=212
wudixiaocaoren 2004-03-20
  • 打赏
  • 举报
回复
你这样作,每次都要读所有的数据,是不可取的。
给你一个简单例子分析一下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace GuestBook
{
/// <summary>
/// View 的摘要说明。
/// </summary>
public class View : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater message;
int pageSize,recordCount,pageCount,currentPage;
protected System.Web.UI.WebControls.Label lbRecordCount;
protected System.Web.UI.WebControls.Label lbCurrentPage;
protected System.Web.UI.WebControls.Label lbPageCount;
protected System.Web.UI.WebControls.LinkButton butPrev;
protected System.Web.UI.WebControls.LinkButton butNext;
protected System.Web.UI.WebControls.DropDownList dlsPageIndex;
OleDbConnection conn;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
pageSize = 5;
string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(".") + "..\\DataBase\\data.aspx";
conn = new OleDbConnection(conStr);
if(!Page.IsPostBack)
{
currentPage = 0; // 初始化当前页为第0页
ViewState["CurrentPage"] = 0;

recordCount = CalculateRecord(); // 计算共有多少条记录
ViewState["RecordCount"] = recordCount;

pageCount = recordCount / pageSize; // 计算共有多少页
ViewState["PageCount"] = pageCount;
InitDls();
BindData();
}
}

public int CalculateRecord()
{
int Count;
string countStr = "Select count(*) as Total from GuestBook";
conn.Open();
OleDbCommand comm = new OleDbCommand(countStr,conn);
OleDbDataReader dr = comm.ExecuteReader();
if(dr.Read())
{
Count = int.Parse(dr["Total"].ToString());
}
else
{
Count = 0;
}
conn.Close();
dr.Close();
return Count;
}

public void BindData()
{
int startPage;
startPage = currentPage * pageSize;
string selectStr = "Select * from GuestBook order by date DESC";
DataSet ds = new DataSet();
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(selectStr,conn);
da.Fill(ds,startPage,pageSize,"Message");
message.DataSource = ds;
message.DataMember = "Message";
message.DataBind();
lbCurrentPage.Text = (currentPage+1).ToString();
lbPageCount.Text = (pageCount+1).ToString();
lbRecordCount.Text = recordCount.ToString();
dlsPageIndex.SelectedIndex = currentPage;
conn.Close();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.butPrev.Click += new System.EventHandler(this.butPrev_Click);
this.butNext.Click += new System.EventHandler(this.butNext_Click);
this.dlsPageIndex.SelectedIndexChanged += new System.EventHandler(this.dlsPageIndex_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void butNext_Click(object sender, System.EventArgs e)
{
currentPage = (int)ViewState["CurrentPage"];
pageCount = (int)ViewState["PageCount"];
recordCount = (int)ViewState["RecordCount"];

if(currentPage<pageCount)
currentPage++;
ViewState["CurrentPage"] = currentPage;
BindData();
}

private void butPrev_Click(object sender, System.EventArgs e)
{
currentPage = (int)ViewState["CurrentPage"];
pageCount = (int)ViewState["PageCount"];
recordCount = (int)ViewState["RecordCount"];

if(currentPage>0)
currentPage--;
ViewState["CurrentPage"] = currentPage;
BindData();
}

public void DelOrRep_Click(object sender,CommandEventArgs e)
{
string commandText;
if(e.CommandName=="Del")
{
if(Session["Admin"] != null)
{
commandText = "Delete from GuestBook where ID = " + e.CommandArgument;
conn.Open();
OleDbCommand comm = new OleDbCommand(commandText,conn);
comm.ExecuteNonQuery();
conn.Close();
Response.Write("<script>alert(\"删除成功!\");</script>");
Response.Redirect("view.aspx");
}
else
Response.Redirect("login.aspx");
}
if(e.CommandName=="Rep")
Response.Redirect("Reply.aspx?GuestID=" + e.CommandArgument);
}

public void InitDls()
{
for(int i=1;i<=(pageCount+1);i++)
{
dlsPageIndex.Items.Add(new ListItem("第"+i+"页",(i-1).ToString()));
}
}
private void dlsPageIndex_SelectedIndexChanged(object sender, System.EventArgs e)
{
currentPage = (int)ViewState["CurrentPage"];
pageCount = (int)ViewState["PageCount"];
recordCount = (int)ViewState["RecordCount"];
currentPage = int.Parse(dlsPageIndex.SelectedItem.Value);
ViewState["CurrentPage"] = currentPage;
BindData();
}

public string GetAddress(string ip)
{
string[] ipArray;
long numString;
string address;
ipArray = ip.Split('.');
numString = (long.Parse(ipArray[0])*256*256*256) + (int.Parse(ipArray[1])*256*256) + (int.Parse(ipArray[2])*256) + int.Parse(ipArray[3])-1;
string conIPStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(".") + "..\\DataBase\\ip.aspx";
string selectStr = "Select 国家,城市 from ipadress where ip1 <= " + numString + " and ip2 >= " + numString;
OleDbDataReader dr;

OleDbConnection connIP = new OleDbConnection(conIPStr);
connIP.Open();
OleDbCommand commIP = new OleDbCommand(selectStr,connIP);
dr = commIP.ExecuteReader();
if(dr.Read())
address = dr["国家"].ToString() + dr["城市"].ToString();
else address = "****";
connIP.Close();
return address;
}
}
}
niaoren 2004-03-20
  • 打赏
  • 举报
回复
太长了,看的累
explorerat2003 2004-03-20
  • 打赏
  • 举报
回复
拜托各位兄弟,我想要知道错在哪里,代码虽多了一点,但很简单,恳求高手相助!!!
???2008 2004-03-20
  • 打赏
  • 举报
回复
看得真累,建议你用分页组件
http://www.webdiyer.com
我也有一个格式和你的差不多。想要的话,发短信给我。
explorerat2003 2004-03-20
  • 打赏
  • 举报
回复
感谢 aquariusBoy(水瓶之心) 的解答,我把MyList的EnableViewState属性改为true也可解决这个问题。

62,266

社区成员

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

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

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

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