DataList分页怎么实现..

madelaop5566 2010-08-15 08:02:22
GridView 有内嵌的分页~,直接改属性就行了

DATALIST 怎么实现分页效果啊,各路大侠帮忙~~~~
效果,性能别太差就行了。。。。。
...全文
889 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhang1struts1yun 2010-08-18
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 begintransaction 的回复:]

引用 4 楼 wuyq11 的回复:
this.DataList1.DataSource =ps ;

DataSet ds=new DataSet();
PagedDataSource objPage=new PagedDataSource();
objPage.DataSource=ds.Tables["user"].DefaultView;
objPage.AllowPagin……
[/Quote]
+1
qq2013 2010-08-18
  • 打赏
  • 举报
回复
一般我是用存储过程的!
begintransaction 2010-08-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wuyq11 的回复:]
this.DataList1.DataSource =ps ;

DataSet ds=new DataSet();
PagedDataSource objPage=new PagedDataSource();
objPage.DataSource=ds.Tables["user"].DefaultView;
objPage.AllowPaging=true;
objP……
[/Quote]
UP
you_fen 2010-08-17
  • 打赏
  • 举报
回复
我们今天才学了,可是觉得还是听复杂的。
SK_Aqi 2010-08-17
  • 打赏
  • 举报
回复
方法确实不少,不过aspnetpager算是封装好的啊,不用写很多代码的
Dearxun 2010-08-17
  • 打赏
  • 举报
回复
没学过
madelaop5566 2010-08-17
  • 打赏
  • 举报
回复
有没封装的分页方法 ~~ 每次分页都要写这么多 。。

性能可想而知~~
gs8716 2010-08-16
  • 打赏
  • 举报
回复
三种方法:

一、数据库分页,推荐此方法特别是大量数据
二、PagedDataSource方法
三、分页控件AspNetPager

搞懂这三个对你以后很有帮助。
脾气不坏 2010-08-15
  • 打赏
  • 举报
回复

建三个Lable控件,ID分别为lb_page,lb_count,lb_currentpage,分别表示共有多少页,共有多少条记录,当前页是多少页
建四个LinkButton控件,ID分别为lbtn_frist,lbtn_up,lbtn_down,lbtn_last,分别表示首页,上一页,下一页,尾页
建一个DropDownList控件,表于选择要跳转到的页
当然肯定要建一个datalist控件,不用设置数据源

前台代码
<p> 共<asp:Label ID="lb_count" runat="server" Text="Label"></asp:Label>记录

<asp:Label ID="lb_page" runat="server" Text="Label"></asp:Label>
页 当前第
<asp:Label ID="lb_currentpage" runat="server" Text="1"></asp:Label>页
<asp:LinkButton ID="lbtn_frist" runat="server" OnClick="lbtn_frist_Click">首页</asp:LinkButton>

<asp:LinkButton ID="lbtn_up" runat="server" OnClick="lbtn_up_Click">上一页</asp:LinkButton>

<asp:LinkButton ID="lbtn_down" runat="server" OnClick="lbtn_down_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lbtn_last" runat="server" OnClick="lbtn_last_Click">尾页</asp:LinkButton>

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList></p>


后台代码
public partial class _Default : System.Web.UI.Page
{
public void Page_Load(object sender, EventArgs e)
{

dlbind();

}
public string GetSub(string str, int length)
{
if (str.Length > length)
{
return str.Substring(0, length) + "。。。";
}
else
{
return str;
}
}

//
public void dlbind() //分页主函数
{
int curpage = Convert.ToInt32(lb_currentpage.Text);

DataTable dt=new DataTable();
DAL.ExecuteSql exec=new DAL.ExecuteSql();
exec.run(dt,"select *from myNews"); //在此设置SQL语句

PagedDataSource ps = new PagedDataSource();
ps.DataSource =dt.DefaultView;
ps.AllowPaging = true;
ps.PageSize = 3; //设置每页显示条数
ps.CurrentPageIndex = curpage - 1;
lb_page.Text = Convert .ToString ( ps.PageCount);
lb_count.Text = ps.DataSourceCount.ToString();
if (!IsPostBack)
{
for (int i = 1; i <= ps.PageCount; i++)
{
DropDownList1.Items.Add(i.ToString());
}
DropDownList1.SelectedItem.Text = curpage.ToString();
}
lbtn_frist.Enabled = true;
lbtn_up.Enabled = true;
lbtn_down.Enabled = true;
lbtn_last.Enabled = true;
if (curpage == 1)
{
lbtn_frist.Enabled = false;
lbtn_up.Enabled = false;
}
if (curpage == ps.PageCount)
{
lbtn_down.Enabled = false;
lbtn_last.Enabled = false;
}
DataList1.DataSource = ps; //绑定数据源
DataList1.DataKeyField = "ArticleID";
DataList1.DataBind();
}

protected void lbtn_frist_Click(object sender, EventArgs e)
{
lb_currentpage.Text = "1";
dlbind();
}

protected void lbtn_up_Click(object sender, EventArgs e)
{
lb_currentpage.Text = Convert.ToString(Convert.ToInt32(lb_currentpage.Text) - 1);
dlbind();
}
protected void lbtn_down_Click(object sender, EventArgs e)
{
lb_currentpage.Text = Convert.ToString(Convert.ToInt32(lb_currentpage.Text) + 1);
dlbind();
}

protected void lbtn_last_Click(object sender, EventArgs e)
{
lb_currentpage.Text = lb_page.Text;
dlbind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int page = Convert.ToInt32(DropDownList1.SelectedItem.Value);
lb_currentpage.Text = page.ToString();
dlbind();
}
}




很好用的。
parverxiao 2010-08-15
  • 打赏
  • 举报
回复
我上面用的是Repeater,可以替换成datalist
parverxiao 2010-08-15
  • 打赏
  • 举报
回复
还可以手写:
前台:
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">下一页</asp:LinkButton>
共<asp:Label ID="lblcount" runat="server" Text="Label"></asp:Label>页  第<asp:Label
ID="lblpage" runat="server" Text="Label"></asp:Label>页    转到<asp:TextBox
ID="TextBox1" runat="server" Width="25px"></asp:TextBox>
<asp:Button ID="Button1" runat="server"
Text="跳转" OnClientClick="return check();" OnClick="Button1_Click" />
<script type="text/javascript">
function check()
{
var page=document.getElementById("<%=TextBox1.ClientID %>");
if(page.value=="")
{
alert("请输入要跳转的页面!");
return false;
}
else
{
var part=/^[1-9]\d*$/;
if(!part.test(page.value))
{
alert("跳转页数格式不正确!");
return false;
}
}
return true;
}

</script>
后台:

/// <summary>
/// 主要用到的控件是Repeater,用到的分页主要是基于PageeDataSource实现分页
/// </summary>
public partial class Default2 : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//调用方法
bind();
}
}
//获取当前页
public int PageIndex
{
get
{
if (ViewState["pageindex"] != null)
return int.Parse(ViewState["pageindex"].ToString());
return 0;
}
set
{
ViewState["pageindex"] = value;
}
}

//获取当前总页数
public int PageCount
{
get
{
if (ViewState["pagecount"] != null)
return int.Parse(ViewState["pagecount"].ToString());
return 1;

}
set
{
ViewState["pagecount"] = value;
}
}

public void bind()
{
//得到的查询结果集
DataSet ds = 你要调用的方法;
if (ds != null)
{
//使用分页类
PagedDataSource ps = new PagedDataSource();
//设置数据源
ps.DataSource = ds.Tables[0].DefaultView;
//设置启用分页
ps.AllowPaging = true;
//设置页大小
ps.PageSize = 10;
//设置当前页
ps.CurrentPageIndex = PageIndex;
Repeater1.DataSource = ps;
Repeater1.DataBind();
//记录总页数
PageCount = ps.PageCount;
this.lblcount.Text = PageCount.ToString();
this.lblpage.Text = (PageIndex + 1).ToString();
SetEnable();
}
}


//用于设置上下页按钮的有效状态
private void SetEnable()
{
//如果是第一页时,"上一页"按钮无效无效,
if (PageIndex == 0)
{
LinkButton1.Enabled = false;
}
else
{
LinkButton1.Enabled = true;
}
//如果是最后一页,"下一页"按钮无效
if (PageIndex + 1 == PageCount)
{
LinkButton2.Enabled = false;
}
else
{
LinkButton2.Enabled = true;
}
}

/// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkButton1_Click(object sender, EventArgs e)
{
PageIndex = PageIndex - 1;
bind();
}
/// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkButton2_Click(object sender, EventArgs e)
{
PageIndex = PageIndex + 1;
bind();
}
/// <summary>
/// 跳转
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
//获取输入的页码
int input = int.Parse(TextBox1.Text);
//判断输入的页码是否大于最大页码
if (input > PageCount)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "a", "alert('超出了最大页!')", true);
return;
}
//将当前页设置为要跳转的页
PageIndex = input - 1;
//重新绑定数据
bind();
}
}

parverxiao 2010-08-15
  • 打赏
  • 举报
回复
可以用分页控件啊AspNetPager
后台:
int PageSize = 5; //每页显示几条信息
/// <summary>
/// 当前显示第几页
/// </summary>
public int PageIndex
{
get
{
if (ViewState["pageindex"] != null)
return int.Parse(ViewState["pageindex"].ToString());
return 1;
}
set { ViewState["pageindex"] = value; }
}
protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
PageIndex = e.NewPageIndex;
bind();

}

/// <summary>
/// 总的一个方法
/// </summary>
private void bind()
{
//自己设置调用方法
int count = int.Parse(ds.Tables[0].Rows[0][0].ToString());
AspNetPager1.CurrentPageIndex = PageIndex;
AspNetPager1.PageSize = PageSize;
AspNetPager1.RecordCount = count;
AspNetPager1.DataBind();
repeater1.DataSource = ds.Tables[1];
repeater1.DataBind();
}
}

porschev 2010-08-15
  • 打赏
  • 举报
回复
aspnetpager+自定义存储过程
wuyq11 2010-08-15
  • 打赏
  • 举报
回复
this.DataList1.DataSource =ps ;

DataSet ds=new DataSet();
PagedDataSource objPage=new PagedDataSource();
objPage.DataSource=ds.Tables["user"].DefaultView;
objPage.AllowPaging=true;
objPage.PageSize=6;
int CurPage;
if(Request.QueryString["Page"]!=null)
CurPage=Convert.ToInt32(Request.QueryString["Page"]);
else
CurPage=1;
objPage.CurrentPageIndex=CurPage-1;
this.lblCurPage.Text="当前页:第"+CurPage.ToString()+"页";
this.DataList1.DataSource=objPage;
this.DataList1.DataBind();
asp.netpager结合PagedDataSource
DataPager+ObjectDataSource
q107770540 2010-08-15
  • 打赏
  • 举报
回复

<% @ 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)
{
CurrentPage = 0;
ViewState[ "PageIndex "] = 0;

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

//计算总共有多少页
if((RecordCount%PageSize)> 0)
PageCount = RecordCount/PageSize+1;
else
PageCount = RecordCount/PageSize;
lblPageCount.Text = PageCount.ToString();
ViewState[ "PageCount "] = PageCount;

ListBind();
}
}
//计算总共有多少条记录
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();

}
[code=HTML]
<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>



参考:http://topic.csdn.net/t/20030108/23/1340730.html




[/code]
beyond_me21 2010-08-15
  • 打赏
  • 举报
回复
百度分页存储过程
threenewbee 2010-08-15
  • 打赏
  • 举报
回复
服务器端分页  - 存储过程
- 应用程序层
- LINQ
- 界面
客户端分页 - JS JQuery ...

62,074

社区成员

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

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

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

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