asp.net中Datalist使用数字分页的实现方法

来源:爱站网时间:2019-09-12编辑:网友分享
DataList控件可以听歌列表的形式显示数据,但显示的多行记录没有分页功能,不方便使用,本文是爱站技术频道小编为大家带来的asp.net中Datalist使用数字分页的实现方法,一起来学习吧!

DataList控件可以听歌列表的形式显示数据,但显示的多行记录没有分页功能,不方便使用,本文是爱站技术频道小编为大家带来的asp.net中Datalist使用数字分页的实现方法,一起来学习吧!

复制代码 代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test(Datalist数字分页).aspx.cs" Inherits="Test_Datalist数字分页_" %>
<!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>
<link href="CSS/CSS.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("mid") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>
</div>
<br />
<div id="PageInfo" runat="server" class="LPageBar"></div>
</form>
</body>
</html>


CS代码:

 

复制代码 代码如下:

 


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Test_Datalist数字分页_ : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//ComFunction cf = new ComFunction();
//DataSet ds = cf.DataBind("M_dizhi");
//this.PageInfo.InnerHtml = PageNums.GetPageNum(ds, DataList1, 12);
this.PageInfo.InnerHtml = PageNums.GetPageSql("M_dizhi", DataList1, 12);
}
}


PageNums.cs

 

复制代码 代码如下:

 


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
/// <summary>
///PageNums 的摘要说明
/// </summary>
public class PageNums
{
/// </summary>
/// <param name="ds">DataSet实例</param>
/// <param name="datalistname">DataList名称</param>
/// <param name="pagesize">分页大小</param>
public static string GetPageNum(DataSet ds, DataList datalistname, int pagesize)
{
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = ds.Tables[0].DefaultView;
objPds.AllowPaging = true;
int total = ds.Tables[0].Rows.Count;
objPds.PageSize = pagesize;
int page;
if (HttpContext.Current.Request.QueryString["page"] != null)
page = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"]);
else
page = 1;
objPds.CurrentPageIndex = page - 1;
datalistname.DataSource = objPds;
datalistname.DataBind();
int allpage = 0;
int next = 0;
int pre = 0;
int startcount = 0;
int endcount = 0;
string pagestr = "";
if (page < 1) { page = 1; }
//计算总页数
if (pagesize != 0)
{
allpage = (total / pagesize);//计算总页数
allpage = ((total % pagesize) != 0 ? allpage + 1 : allpage);
allpage = (allpage == 0 ? 1 : allpage);
}
next = page + 1;
pre = page - 1;
startcount = (page + 5) > allpage ? allpage - 9 : page - 4;//中间页起始序号
//中间页终止序号
endcount = page < 5 ? 10 : page + 5;
if (startcount < 1) { startcount = 1; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始
if (allpage < endcount) { endcount = allpage; } //页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内
pagestr = "共" + allpage + "页";
pagestr += page > 1 ? "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=1\">首页</a>;<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + pre + "\">上一页</a>" : "首页 上一页";
//中间页处理,这个增加时间复杂度,减小空间复杂度
for (int i = startcount; i <= endcount; i++)
{
pagestr += page == i ? "<font color=\"#ff0000\">" + i + "</font>" : "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + i + "\">" + i + "</a>";
}
pagestr += page != allpage ? "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + next + "\">下一页</a><a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + allpage + "\">末页</a>" : " 下一页 末页";
return pagestr;
}

public static string GetPageSql(string FileName, DataList datalistname, int pagesize)
{
int page;
int allpage = 0;
int next = 0;
int pre = 0;
int startcount = 0;
int endcount = 0;
string pagestr = "";
if (HttpContext.Current.Request.QueryString["page"] != null)
page = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"]);
else
page = 1;
if (page < 1) { page = 1; }
DataSet ds = DataBindSql("EXEC pagesql '*',' FROM " + FileName + " '," + pagesize + "," + page, FileName);
datalistname.DataSource = ds;
datalistname.DataBind();
int total = DataBind(FileName);
//计算总页数
if (pagesize != 0)
{
allpage = (total / pagesize);//计算总页数
allpage = ((total % pagesize) != 0 ? allpage + 1 : allpage);
allpage = (allpage == 0 ? 1 : allpage);
}
next = page + 1;
pre = page - 1;
startcount = page / 5 * 5;//中间页起始序号
//中间页终止序号
endcount = startcount+5;
if (startcount < 1) { startcount = 1; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始
if (allpage < endcount) { endcount = allpage; } //页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内
pagestr = "<a>"+page + "/" + allpage + "页</a>";
pagestr += page > 1 ? "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=1\">首页</a><a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + pre + "\">上一页</a>" : "首页 上一页";
//中间页处理,这个增加时间复杂度,减小空间复杂度
for (int i = startcount; i <= endcount; i++)
{
pagestr += page == i ? "<font color=\"#ff0000\">" + i + "</font>" : "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + i + "\">" + i + "</a>";
}
pagestr += page != allpage ? "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + next + "\">下一页</a><a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + allpage + "\">末页</a>" : " 下一页 末页";
return pagestr;
}
private static int DataBind(string FileName)
{
string sql = "select * from " + FileName;
DbConnection dc = new DbConnection();
SqlConnection mycon = new SqlConnection(dc.ConnectionString);
SqlDataAdapter mypter = new SqlDataAdapter(sql, mycon);
DataSet ds = new DataSet();
mycon.Open();
mypter.Fill(ds, FileName);
mycon.Close();
int total = ds.Tables[0].Rows.Count;
return total;
}
private static DataSet DataBindSql(string sql, string FileName)
{
DbConnection dc = new DbConnection();
SqlConnection mycon = new SqlConnection(dc.ConnectionString);
SqlDataAdapter mypter = new SqlDataAdapter(sql,mycon);
DataSet ds = new DataSet();
mycon.Open();
mypter.Fill(ds, FileName);
mycon.Close();
return ds;
}
}


存储过程pagesql

 

 

以上就是爱站技术频道小编给大家介绍的asp.net中Datalist使用数字分页的实现方法,关于Datalist的实现方法,相信大家都介绍了很多了,有需要的朋友可以参考一下。 

上一篇:C#反射技术的简单操作

下一篇:JS实现完美include加载功能代码

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载