DataList分页问题

zxd9208 2011-03-25 06:35:19
上代码:数据绑定成功了 就是一直都是显示第一页内容 “首页 上一页 下一页 末页 跳转 ”功能都没有一个实现的 这是为什么求高手看看


 private string myConn = ConfigurationManager.ConnectionStrings["ClientsConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Bind(0);
}

//创建一个分页数据源的对象且一定要声明为静态
protected static PagedDataSource ps = new PagedDataSource();
private void Bind(int CurrentPage)
{
string selectSql = "select ClientID,ClientName,AddressStr,Email from OrderClient where ClientID<=700";

SqlConnection connectiong = new SqlConnection(myConn);
SqlDataAdapter da = new SqlDataAdapter(selectSql, connectiong);

DataSet ds = new DataSet();
da.Fill(ds, "OrderClient");

DataList1.DataSource = ds;
DataList1.DataBind();
ps.DataSource = ds.Tables["OrderClient"].DefaultView;
ps.AllowPaging = true;//是否可以分页
ps.PageSize = 4;
ps.CurrentPageIndex = CurrentPage;//取得当前页码

DataList1.DataSource = ps;
DataList1.DataKeyField = "ClientID";
DataList1.DataBind();
}


protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
switch (e.CommandName)
{
//以下5种情况分别为捕获用户点击第一页、上一页、下一页、最后一页和页面跳转是发生的事件
case "first":
ps.CurrentPageIndex = 0;
Bind(ps.CurrentPageIndex);
break;
case "pre":
ps.CurrentPageIndex = ps.CurrentPageIndex - 1;
Bind(ps.CurrentPageIndex);
break;
case "next":
ps.CurrentPageIndex = ps.CurrentPageIndex + 1;
Bind(ps.CurrentPageIndex);
break;
case "last":
ps.CurrentPageIndex = ps.PageCount - 1;
Bind(ps.CurrentPageIndex);
break;
case "search":
if (e.Item.ItemType == ListItemType.Footer)
{
int PageCount = int.Parse(ps.PageCount.ToString());
TextBox txtPage = e.Item.FindControl("txtPage") as TextBox;
int myPageNum = 0;
if (!txtPage.Text.Equals(""))
{
myPageNum = Convert.ToInt32(txtPage.Text.ToString());
if (myPageNum <= 0 || myPageNum > PageCount)
Response.Write("<script>alert('输入页数没有超出页数总数')</script>");
else

Bind(myPageNum - 1);
}

}
break;

}
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
//得到脚模板中的控件。并创建变量
Label CurrentPage = e.Item.FindControl("lblNowPage") as Label;
Label PageCount = e.Item.FindControl("lblCount") as Label;
LinkButton FirstPage = e.Item.FindControl("lnkbtnFirst") as LinkButton;
LinkButton PrePage = e.Item.FindControl("lnkbtnFont") as LinkButton;
LinkButton NextPage = e.Item.FindControl("lnkbtnNext") as LinkButton;
LinkButton LastPage = e.Item.FindControl("lnkbtnLast") as LinkButton;
CurrentPage.Text = (ps.CurrentPageIndex + 1).ToString();//绑定显示当前页
PageCount.Text = ps.PageCount.ToString();//绑定显示总页数
if (ps.IsFirstPage)
{
FirstPage.Enabled = false;
PrePage.Enabled = false;
}
if (ps.IsLastPage)
{
NextPage.Enabled = false;
LastPage.Enabled = false;
}
}
}
...全文
138 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
子夜__ 2011-03-26
  • 打赏
  • 举报
回复
//后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page
{
int pagesize = 3;
protected void Page_Load(object sender, EventArgs e)
{

}
private void binddatalist()
{
int index = Int32.Parse(ViewState["pageindex"].ToString());
int count = Int32.Parse(ViewState["pagecount"].ToString());
SqlConnection conn = new SqlConnection("server=.;database=MSSTB;uid=sa;pwd=sa");
SqlDataAdapter da = new SqlDataAdapter("select * from Product2", conn);
conn.Open();
DataSet ds = new DataSet();
da.Fill(ds, index * pagesize, pagesize, "Product2");
Label1.Text = "第" + (index + 1) + "页";
LinkButton1.Enabled = true;
LinkButton2.Enabled = true;
LinkButton3.Enabled = true;
LinkButton4.Enabled = true;
if (count == 1)
{
LinkButton1.Enabled = false;
LinkButton2.Enabled = false;
LinkButton3.Enabled = false;
LinkButton4.Enabled = false;
Label1.Text = "当前只有一页";
}
else
{
if (index == 0)
{
LinkButton1.Enabled = false;
LinkButton2.Enabled = false;
}
if (index == count - 1)
{
LinkButton4.Enabled = false;
LinkButton3.Enabled = false;
}
}
DataList1.DataSource = ds.Tables["Product2"].DefaultView;
DataList1.DataBind();
}
public int CalculateRecord()//返回行数
{
int intCount;
string strCount = "select count(*) as c from Product2";
SqlConnection Con = new SqlConnection("server=.;database=MSSTB;uid=sa;pwd=sa");
SqlCommand addCommand = new SqlCommand(strCount, Con);
addCommand.Connection.Open();
SqlDataReader dr;
dr = addCommand.ExecuteReader();
if (dr.Read())
{
intCount = Int32.Parse(dr["c"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
}
protected void putdown(Object sender, CommandEventArgs e)//按钮事件
{
int index = Int32.Parse(ViewState["pageindex"].ToString());
int count = Int32.Parse(ViewState["pagecount"].ToString());
string cmd = e.CommandName;
switch (cmd)
{
case "first": index = 0;
break;
case "next":
if (index < (count - 1)) index++;
break;
case "up":
if (index > 0) index--;
break;
case "end": index = count - 1;
break;
}
ViewState["pageindex"] = index;
binddatalist();
}
protected void Page_Init(object sender, EventArgs e)
{

int recordcount = CalculateRecord();//总数
int pagecount = recordcount / pagesize;
//如果有多余记录,则页面总数加一
if (recordcount % pagesize != 0)
{
pagecount += 1;
}
ViewState["pagecount"] = pagecount;//总行数
ViewState["pageindex"] = 0;//当前索引
binddatalist();
}
}




//前台
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Pname")%>
</ItemTemplate>
</asp:DataList>

</div>
行数  
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
 <br />
页数  
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<br />
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="first" oncommand="putdown">首页</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="up" oncommand="putdown">上一页</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="next" oncommand="putdown">下一页</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" CommandName="end" oncommand="putdown">尾页</asp:LinkButton>
</form>
</body>
</html>



Corporal 2011-03-26
  • 打赏
  • 举报
回复
lz关键错误是没有可靠的记录当前是第几页。

ps在每次访问时都会初始化。


有空看下页面的生命周期,还是很有用的。

zxd9208 2011-03-26
  • 打赏
  • 举报
回复
嗯~自己解决了!原来没有把 那几个功能键放在DataList控件中
HelloJimmy520 2011-03-26
  • 打赏
  • 举报
回复
功能键为什么要放在DataList中呢?

这有什么关系么?你当前页数 一般放在viewstate中
shujian158 2011-03-25
  • 打赏
  • 举报
回复
菜鸟路过,用Gridview自带分页功能,不用写后台代码。
zhongweng 2011-03-25
  • 打赏
  • 举报
回复
你代码中好像没得总共的数据条数吧
zxd9208 2011-03-25
  • 打赏
  • 举报
回复
有木有人( ⊙ o ⊙ )啊!

62,046

社区成员

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

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

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

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