看了网上一些关于DataList分页的列子,有很多分页方法,主要有利用PageDataSource类,存储过程分页,还有就是利用DataSet进行分页,其实是利用SqlDataAdapter的Fill方法,下面我就用DataSet 就行分页与大家分享(其实也是网上看来的,然后加上自己的理解,多了一点点功能,呵呵)
.cs文件如下:
int pageSize, pageCount, recordCount, currentPage;
protected void Page_Load(object sender, EventArgs e)
{
pageSize = 10;
if (!IsPostBack)
{
ListBind();//绑定DataList
currentPage = 0;//首页索引为0
ViewState["pageIndex"] = 0;//保存页索引
//总记录数
recordCount = CalculateRecord();
lblRecordCount.Text ="总记录数:"+ recordCount.ToString();
//总页数
pageCount = recordCount / pageSize;//取整
if (recordCount % pageSize > 0) //总记录数不是 页大小的 整数倍
{
pageCount = pageCount + 1;
}
ViewState["pageCount"] = pageCount; //保存页数
lblPageCount.Text = "页数:" + pageCount.ToString();
Label2.Text = (currentPage + 1) + "/" + pageCount.ToString();
}
btnGo.Attributes.Add("onclick", " return validate()");
}
//计算记录总数
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from quote";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(strCount, con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
intCount = int.Parse(dr["co"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
con.Close();
}
//创建数据源
ICollection CreateSource()
{
SqlConnection con=new SqlConnection(connectionString);
int startIndex;
startIndex = currentPage * pageSize; //某页的第一条记录索引
string strSel = "select * from quote";
DataSet ds = new DataSet();
SqlDataAdapter da=new SqlDataAdapter(strSel,con);
da.Fill(ds, startIndex, pageSize, "quote");//取一张页面的记录数
return ds.Tables["quote"].DefaultView;
}
public void ListBind()
{
DataList1.DataSource = CreateSource();
DataList1.DataBind();
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if (currentPage == pageCount - 1)
{
lbnNextPage.Enabled = false;
}
if (currentPage == 0)
{
lbnPrevPage.Enabled = false;
}
}
//上一页 下一页
protected void Page_OnClick(object sender, CommandEventArgs e)
{
currentPage = (int)ViewState["pageIndex"];
pageCount = (int)ViewState["pageCount"];
string cmd = e.CommandName;
switch (cmd)
{
case "Next":
if (currentPage < pageCount - 1) currentPage++;
//Label2.Text = (currentPage+1) + "/" + pageCount.ToString();
break;
case "Prev":
if (currentPage > 0) currentPage--;
//Label2.Text = (currentPage+1) + "/" + pageCount.ToString();
break;
case "First":
currentPage = 0;
break;
case"Last":
currentPage = pageCount - 1;
break;
}
Label2.Text = (currentPage + 1) + "/" + pageCount.ToString();
ViewState["pageIndex"] = currentPage;
ListBind();
}
//跳转页
protected void btnGo_Click(object sender, EventArgs e)
{
int intPage=Convert.ToInt32(txtPage.Text);
//currentPage = intPage-1;
pageCount = (int)ViewState["pageCount"];
if (intPage > pageCount) //如果输入的页数大于总页数
{
currentPage = pageCount - 1;
}
else if (intPage <=0)
{
currentPage = 0;
}
else
{
currentPage = intPage - 1;
}
Label2.Text = (currentPage + 1) + "/" + pageCount.ToString();
ListBind();
}
aspx 文件如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>021DataList</title>
</HEAD>
<body>
<script type="text/javascript">
function validate()
{
if (document.getElementById("txtPage").value=="")
{
alert('不能为空!');
document.getElementById("txtPage").focus();
return false;
}
var digits="0123456789";
var temp;
for(i=0;document.getElementById("txtPage").value.length;i++)
{
temp=document.getElementById("txtPage").value.substring(i,i+1);
if (digits.indexOf(temp)==-1)
{
alert("只能输入数字!");
document.getElementById("txtPage").focus();
return false;
}
return true;
}
return true;
}
</script>
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("printname") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></FONT><asp:LinkButton ID="lbnPrevPage" runat="server" CommandName="Prev" OnCommand="Page_OnClick">上一页</asp:LinkButton>
<asp:LinkButton ID="lbnNextPage" runat="server" CommandName="Next" OnCommand="Page_OnClick">下一页</asp:LinkButton>
<asp:Label ID="lblRecordCount" runat="server" Text="Label"></asp:Label>
<asp:Label ID="lblPageCount" runat="server" Text="Label"></asp:Label>
<asp:LinkButton ID="lbnFirstPage" runat="server" CommandName="First" OnCommand="Page_OnClick">首页</asp:LinkButton>
<asp:LinkButton ID="lbnLastPage" runat="server" CommandName="Last" OnCommand="Page_OnClick" Width="33px">末页</asp:LinkButton>
跳转到<asp:TextBox ID="txtPage" runat="server" Width="30px"></asp:TextBox>页
<asp:Button ID="btnGo" runat="server" Text="Go" OnClick="btnGo_Click" />
</form>
</body>
</HTML>