有没有什么好方法用于DataList分页的

redcomet2004 2004-07-28 10:24:08
在用DataAdapter填充DataSet时可以写入指定条数来实现,但这种方法效率不是很高,有没有什么其他的好方法来实现?
...全文
290 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
iceqings 2005-07-12
  • 打赏
  • 举报
回复
下面是全部原代码,其实用到的方法和PHP中的分页差不多,只是这里用的是DataAdapter与DataSet组合,而不是PHP中的SQL语句直接搞定。

(本程序在.Net Framework Beta 2下测试通过)


<% @ Page Language="C#" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.OleDb" %>
<Script Language="C#" Runat="Server">
/*
Create By 飞刀
http://www.aspcn.com
2001-7-25 01:44

Support .Net Framework Beta 2
*/
OleDbConnection MyConn;
int PageSize,RecordCount,PageCount,CurrentPage;
public void Page_Load(Object src,EventArgs e)
{
//设定PageSize
PageSize = 10;

//连接语句
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".")+"..\\DataBase\\db1.mdb;";
MyConn = new OleDbConnection(MyConnString);
MyConn.Open();

//第一次请求执行
if(!Page.IsPostBack)
{
ListBind();
CurrentPage = 0;
ViewState["PageIndex"] = 0;

//计算总共有多少记录
RecordCount = CalculateRecord();
lblRecordCount.Text = RecordCount.ToString();

//计算总共有多少页
PageCount = RecordCount/PageSize;
lblPageCount.Text = PageCount.ToString();
ViewState["PageCount"] = PageCount;
}
}
//计算总共有多少条记录
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from Score";
OleDbCommand MyComm = new OleDbCommand(strCount,MyConn);
OleDbDataReader dr = MyComm.ExecuteReader();
if(dr.Read())
{
intCount = Int32.Parse(dr["co"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
}

ICollection CreateSource()
{

int StartIndex;

//设定导入的起终地址
StartIndex = CurrentPage*PageSize;
string strSel = "select * from Score";
DataSet ds = new DataSet();

OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);
MyAdapter.Fill(ds,StartIndex,PageSize,"Score");

return ds.Tables["Score"].DefaultView;
}
public void ListBind()
{
score.DataSource = CreateSource();
score.DataBind();

lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;
if(CurrentPage==0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage+1).ToString();

}

public void Page_OnClick(Object sender,CommandEventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];
PageCount = (int)ViewState["PageCount"];

string cmd = e.CommandName;
//判断cmd,以判定翻页方向
switch(cmd)
{
case "next":
if(CurrentPage<(PageCount-1)) CurrentPage++;
break;
case "prev":
if(CurrentPage>0) CurrentPage--;
break;
}

ViewState["PageIndex"] = CurrentPage;

ListBind();

}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form runat="server">
共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录
当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页

<asp:DataList id="score" runat="server"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="Gainsboro"
EditItemStyle-BackColor="yellow"
>
<ItemTemplate>
姓名:<%# DataBinder.Eval(Container.DataItem,"Name") %>
<asp:LinkButton id="btnSelect" Text="编辑" CommandName="edit" runat="server" />
</ItemTemplate>
</asp:DataList>
<asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" />
<asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" />

</form>
</body>
</html>
iceqings 2005-07-12
  • 打赏
  • 举报
回复
下面是全部原代码,其实用到的方法和PHP中的分页差不多,只是这里用的是DataAdapter与DataSet组合,而不是PHP中的SQL语句直接搞定。

(本程序在.Net Framework Beta 2下测试通过)


<% @ Page Language="C#" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.OleDb" %>
<Script Language="C#" Runat="Server">
/*
Create By 飞刀
http://www.aspcn.com
2001-7-25 01:44

Support .Net Framework Beta 2
*/
OleDbConnection MyConn;
int PageSize,RecordCount,PageCount,CurrentPage;
public void Page_Load(Object src,EventArgs e)
{
//设定PageSize
PageSize = 10;

//连接语句
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".")+"..\\DataBase\\db1.mdb;";
MyConn = new OleDbConnection(MyConnString);
MyConn.Open();

//第一次请求执行
if(!Page.IsPostBack)
{
ListBind();
CurrentPage = 0;
ViewState["PageIndex"] = 0;

//计算总共有多少记录
RecordCount = CalculateRecord();
lblRecordCount.Text = RecordCount.ToString();

//计算总共有多少页
PageCount = RecordCount/PageSize;
lblPageCount.Text = PageCount.ToString();
ViewState["PageCount"] = PageCount;
}
}
//计算总共有多少条记录
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from Score";
OleDbCommand MyComm = new OleDbCommand(strCount,MyConn);
OleDbDataReader dr = MyComm.ExecuteReader();
if(dr.Read())
{
intCount = Int32.Parse(dr["co"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
}

ICollection CreateSource()
{

int StartIndex;

//设定导入的起终地址
StartIndex = CurrentPage*PageSize;
string strSel = "select * from Score";
DataSet ds = new DataSet();

OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);
MyAdapter.Fill(ds,StartIndex,PageSize,"Score");

return ds.Tables["Score"].DefaultView;
}
public void ListBind()
{
score.DataSource = CreateSource();
score.DataBind();

lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;
if(CurrentPage==0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage+1).ToString();

}

public void Page_OnClick(Object sender,CommandEventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];
PageCount = (int)ViewState["PageCount"];

string cmd = e.CommandName;
//判断cmd,以判定翻页方向
switch(cmd)
{
case "next":
if(CurrentPage<(PageCount-1)) CurrentPage++;
break;
case "prev":
if(CurrentPage>0) CurrentPage--;
break;
}

ViewState["PageIndex"] = CurrentPage;

ListBind();

}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form runat="server">
共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录
当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页

<asp:DataList id="score" runat="server"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="Gainsboro"
EditItemStyle-BackColor="yellow"
>
<ItemTemplate>
姓名:<%# DataBinder.Eval(Container.DataItem,"Name") %>
<asp:LinkButton id="btnSelect" Text="编辑" CommandName="edit" runat="server" />
</ItemTemplate>
</asp:DataList>
<asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" />
<asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" />

</form>
</body>
</html>
jxyctian 2004-11-09
  • 打赏
  • 举报
回复
這是我剛做完的專案:的實例,100%有用.
private void PageLoadMode()
{
ViewState["pagesize"]=1;
ViewState["currentindex"]=0;
QueryDataList();
}
bool QueryDataList()
{
string Sql="select count(*) from AA";
SqlConnection conn=new SqlConnection();
SqlCommand com=new SqlCommand(Sql,conn);

conn.ConnectionString=gb.connString;
conn.Open();
recordcount=(int)com.ExecuteScalar();
conn.Close();


pagecount=recordcount/(int)ViewState["pagesize"];
if(recordcount/(int)ViewState["pagesize"]>0)
{
}
ViewState["pagecount"]=pagecount;

DataSet ds=new DataSet();
// Trace.Warn(DataList1.CurrentPageIndex.ToString());
string Sql1="select name,zhanghao,GetWay,IsYesNo from AA";
da=new SqlDataAdapter(Sql1,conn) ;
da.Fill(ds,((int)ViewState["currentindex"])*((int)ViewState["pagesize"]),((int)ViewState["pagesize"]),"AA");
MyTable=ds.Tables [0];
DataList1.DataSource=ds.Tables[0] ;
DataList1.DataBind();


for(int i=0;i<MyTable.Rows.Count;i++ )
{
CheckBox ChB=(CheckBox)DataList1.Items[i].FindControl("IsYesNo");

if(MyTable.Rows[i]["IsYesNo"].ToString() =="1" )
ChB.Checked=true;
else if (MyTable.Rows[i]["IsYesNo"].ToString() =="0")
ChB.Checked =false;
}

GetDataGridDropDownList();

for(int index=0;index<DataList1.Items.Count;index++)
{
DropDownList ddl_GetWay1 = (DropDownList)DataList1.Items[index].FindControl("GetWay");
if(MyTable.Rows[index]["GetWay"].ToString()=="")
{
}
else
{ddl_GetWay1.SelectedValue=MyTable.Rows[index]["GetWay"].ToString().Trim();}
}

return true;
}
private void DataList1_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{

aaa=e.Item.ItemIndex;
string name=((TextBox) e.Item.FindControl ("name")).Text ;
string zhanghao=((TextBox) e.Item.FindControl ("zhanghao")).Text ;
string GetWay=((DropDownList) e.Item.FindControl ("GetWay")).SelectedValue ;
CheckBox ChB=(CheckBox) e.Item.FindControl ("IsYesNo");

MyTable.Rows[aaa]["name"]=name;
MyTable.Rows[aaa]["zhanghao"]=zhanghao;
MyTable.Rows[aaa]["GetWay"]=GetWay;
if (ChB.Checked==true )
{MyTable.Rows[aaa]["IsYesNo"]=1;}
else
{MyTable.Rows[aaa]["IsYesNo"]=0;}


UpdateAA();
QueryDataList();
}
public void GetDataGridDropDownList()
{
string[] Array2=new string[2];
Array2[0]="";
Array2[1]="B030";
ds2.Reset();
ds2=gb.ExecQuery("SP_CASH_M2_01_Q6", ref Array2);

//DataGrid
for(int i=0;i<DataList1.Items.Count;i++)
{
//定義DataGrid所有DropDownList
DropDownList ddl_GetWay=(DropDownList) DataList1.Items[i].FindControl("GetWay");

//判斷有無資料
ddl_GetWay.DataSource=ds2.Tables[0];
ddl_GetWay.DataTextField =ds2.Tables[0].Columns["TaO1_TxtExplain"].ToString().Trim();
ddl_GetWay.DataValueField=ds2.Tables[0].Columns["TaO1_TxtExplain"].ToString().Trim();
ddl_GetWay.DataBind();
}
}
private void first_Click(object sender, System.EventArgs e)
{
if((int)ViewState["currentindex"]>0)
{
ViewState["currentindex"]=0;
QueryDataList();
}
}

private void next_Click(object sender, System.EventArgs e)
{
if((int)ViewState["currentindex"]<(int)ViewState["pagecount"]-1)
{
ViewState["currentindex"]=(int)ViewState["currentindex"]+1;
QueryDataList();
}

}
private void perv_Click(object sender, System.EventArgs e)
{
if((int)ViewState["currentindex"]>0)
{
ViewState["currentindex"]=(int)ViewState["currentindex"]-1;
QueryDataList();
}
}

private void end_Click(object sender, System.EventArgs e)
{
if((int)ViewState["currentindex"]<(int)ViewState["pagecount"])
{
ViewState["currentindex"]=(int)ViewState["pagecount"]-1;
QueryDataList();
}
}
张海霖 2004-11-09
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/TopicView.asp?id=3527318
wdfboy1978 2004-07-30
  • 打赏
  • 举报
回复
使用PagedDataSource

private void BindData()
{
string sqlstr="SELECT n.id, n.title, s.bigclass, s.smallclass, n.addtime,lettop = CASE WHEN n.lettop = 0 THEN '否' ELSE '<font color=red>是</font>' END FROM news n INNER JOIN news_style s ON s.smallclass_no = n.smallclass_no WHERE (n.checked = 1) ORDER BY n.addtime DESC";
DataSet ds=SqlHelper.ExecuteDataset(Connstr,CommandType.Text,sqlstr);

pp=new PagedDataSource();
pp.DataSource=ds.Tables[0].DefaultView;
pp.AllowPaging=true;
pp.PageSize=15;
lblTotalPage.Text =pp.PageCount.ToString();

int CPages;
if(T_Pages!=null)
{
CPages=Convert.ToInt32(T_Pages);
}
else
{
if(Request.QueryString["page"]!=null)
{
CPages=Convert.ToInt32(Request.QueryString["page"]);
}
else
CPages=1;
}

pp.CurrentPageIndex=CPages-1;
lblCurrentPage.Text = CPages.ToString();
lblTotalCount.Text=Convert.ToString(pp.PageCount*pp.PageSize);
if (Convert.ToInt32(lblTotalPage.Text)!=1)
{
first.NavigateUrl=Request.CurrentExecutionFilePath + "?page=1";
pre.NavigateUrl=Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(CPages-1);
next.NavigateUrl=Request.CurrentExecutionFilePath+ "?page=" + Convert.ToString(CPages+1);
last.NavigateUrl=Request.CurrentExecutionFilePath+ "?page=" + lblTotalPage.Text.ToString();

if(CPages==1) //当前页是首页
pre.Enabled=false;

if(CPages==pp.PageCount) //当前页是尾页
next.Enabled=false;
}
else
{
first.Enabled=false;
pre.Enabled=false;
next.Enabled=false;
last.Enabled=false;
}

DataList1.DataSource=pp;
DataList1.DataBind();
}
wdengwei 2004-07-29
  • 打赏
  • 举报
回复
http://dotnet.aspx.cc/ShowDetail.aspx?id=47DD34FD-ED0B-4455-94E8-CD9F63E19893

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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