如题
解决方案 »
- 怎样实现QQ空间地址那样的域名
- Windows7 如何使用IIS7的MMC嵌入工具修改配置??
- 大牛进!跟技术主管发生分歧了,MVC中的Controller是否可以使用继承来写Action??
- web.config中roleManager的配置问题,谢谢解答
- 救火啦,一个string()引用的时候提示“未将对象引用设置到对象的实例”这么处理
- 关于如何使用Javascript从DataGrid中取值的问题(急救!!不够我再加分!)
- asp。net 怎么获取多个代理的ip地址
- 困惑中。。。
- string strSql = string.Format("insert into Special_Order (SpecialProduct_Name,Sp
- 高手之路:ASP.Net的锻炼
- PETSHOP 中 SQLHelper 类中的 ExecuteReader() 为什麽有 cmd.Parameters.Clear(); ???
- 关于button
ASP.NET2.0中datalist仿百度分页(和google差不多)
我本人做的时候用的是MYSQL数据库, 测试时请转化到MSSQL数据库即可
注意此方法在ASP.NET1.0或1.1中测试通不过,但是基本方法是没问题,关键是掌握其分页原理
我做的时候用的是mysql数据库,如果你用的是mssql话就吧所有以mysql开头的换成sql就可以了using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;
using System.IO;
public partial class mysql : System.Web.UI.Page
{
MySqlConnection conn = new MySqlConnection(System.Configuration.ConfigurationManager.AppSettings["constrmy"]);
int ToatalCountRecord;//总记录数
int PageItem = 4;//每页显示的条数
int CurrentPage = 1;//当前页数
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
if (Request.QueryString["page"] != null)
{
if (!Int32.TryParse(Request.QueryString["page"].ToString(), out CurrentPage))
{
Response.Write("请输入分页参数!");
Response.End();
return;
}
}
this.BuidGrid();
}
}
private void BuidGrid()
{
string s2 = "select * from ts1";
MySqlDataAdapter da = new MySqlDataAdapter(s2, conn);
DataSet ds = new DataSet();
int startRecord = (CurrentPage - 1) * PageItem;
da.Fill(ds, startRecord, PageItem, "a");
this.DataList1.DataSource = ds.Tables["a"].DefaultView;
this.DataList1.DataBind();
MySqlCommand comm = new MySqlCommand("select count(*) from ts1", conn);
conn.Open();
ToatalCountRecord = Convert.ToInt32(comm.ExecuteScalar());
conn.Close();
BuildPages();
}
private void BuildPages()
{
int Step = 5;//偏移量
int LeftNum = 0;//做界限
int RightNum = 0;//右界限
string PageUrl = Request.FilePath;
int PageCount = (int)Math.Ceiling((double)(ToatalCountRecord) / PageItem);
if (CurrentPage - Step < 1)
{
LeftNum = 1;
}
else
{
LeftNum = CurrentPage - Step;
}
if (CurrentPage + Step > PageCount)
{
RightNum = PageCount;
}
else
{
RightNum = CurrentPage + Step;
}
string OutPut = "";
if (CurrentPage > 1)
{
OutPut += "<a href='" + PageUrl + "?page=" + (CurrentPage - 1) + "'>" + "上一页" + "</a>";
}
for (int i = LeftNum; i <= RightNum; i++)
{
if (i == CurrentPage)
{
OutPut += "<font color=red>" + " " +"["+i.ToString() +"]"+ "" + "</font>";
}
else
{
OutPut += "<a href='" + PageUrl + "?page=" + i.ToString() + "'>" + " " +"["+ i.ToString() +"]"+ " " + "</a>";
}
}
if (CurrentPage < PageCount)
{
OutPut += "<a href='" + PageUrl + "?page=" + (CurrentPage + 1) + "'>" + "下一页" + "</a>";
}
this.PageInfo.InnerHtml = OutPut;
}
}
需要在前台添加一个 <div id="PageInfo" runat="server" >
/// 创建并显示分页器
/// </summary>
private void BuildPager(int totalRecords,int currentPage,int pageSize,int pid)
{
int alter = 4 ;
int startPage = 1 ;
int endPage = currentPage + alter ;
int totalPages = this.CalculateTotalPages(totalRecords,pageSize) ; if(currentPage > alter)
{
startPage = currentPage - alter ;
} if(endPage > totalPages)
{
endPage = totalPages ;
} string strTemp = @"<a href='PhotoList.aspx?pid={0}&pno={1}'>{2}</a> " ;
StringBuilder sb = new StringBuilder("") ;
if(currentPage != startPage)
{
sb.Append( string.Format( strTemp , pid , 1 , "上一页" ) ) ;
} for( int i = startPage ; i <= endPage ; i++ )
{
if( currentPage == i )
{
sb.Append("<font color=red>" + i + "</font> ") ;
}
else
{
sb.Append( string.Format( strTemp , pid , i ,"[" + i + "]" ) ) ;
}
} if(currentPage != endPage)
{
sb.Append( string.Format( strTemp , pid , currentPage + 1 , "下一页") ) ;
} this.ltlShowPager.Text = sb.ToString() ;
} /**//// <summary>
/// 计算总页数
/// </summary>
/// <param name="totalRecords">总记录数</param>
/// <param name="pageSize">每页记录数</param>
private int CalculateTotalPages(int totalRecords, int pageSize)
{
int totalPagesAvailable; totalPagesAvailable = totalRecords / pageSize; //由于C#的整形除法 会把所有余数舍入为0,所以需要判断是否需要加1
if ((totalRecords % pageSize) > 0)
totalPagesAvailable++; return totalPagesAvailable;
}
SqlConnection conn = new SqlConnection(constr);
int ToatalCountRecord;//总记录数
int PageItem = 4;//每页显示的条数
protected System.Web.UI.HtmlControls.HtmlGenericControl PageInfo;
int CurrentPage = 1;//当前页数
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataGridBind();
} }
public void DataGridBind()
{
string s2 = "select * from corp_users";
SqlDataAdapter da = new SqlDataAdapter(s2, conn);
DataSet ds = new DataSet();
int startRecord = (CurrentPage - 1) * PageItem;
da.Fill(ds, startRecord, PageItem, "a");
this.DataList1.DataSource = ds.Tables["a"].DefaultView;
this.DataList1.DataBind();
SqlCommand comm = new SqlCommand("select count(*) from corp_users", conn);
conn.Open();
ToatalCountRecord = Convert.ToInt32(comm.ExecuteScalar());
conn.Close();
BuildPages(); } private void BuildPages()
{
int Step = 5;//偏移量
int LeftNum = 0;//做界限
int RightNum = 0;//右界限
string PageUrl = Request.FilePath;
int PageCount = (int)Math.Ceiling((double)(ToatalCountRecord) / PageItem);
if (CurrentPage - Step < 1)
{
LeftNum = 1;
}
else
{
LeftNum = CurrentPage - Step;
}
if (CurrentPage + Step > PageCount)
{
RightNum = PageCount;
}
else
{
RightNum = CurrentPage + Step;
}
string OutPut = "";
if (CurrentPage > 1)
{
OutPut += "<a href='" + PageUrl + "?page=" + (CurrentPage - 1) + "'>" + "上一页" + "</a>";
}
for (int i = LeftNum; i <= RightNum; i++)
{
if (i == CurrentPage)
{
OutPut += "<font color=red>" + " " + "[" + i.ToString() + "]" + "" + "</font>";
}
else
{
OutPut += "<a href='" + PageUrl + "?page=" + i.ToString() + "'>" + " " + "[" + i.ToString() + "]" + " " + "</a>";
}
}
if (CurrentPage < PageCount)
{
OutPut += "<a href='" + PageUrl + "?page=" + (CurrentPage + 1) + "'>" + "下一页" + "</a>";
}
this.pageInfo.InnerHtml = OutPut;
}可是我点下一页都没反应!!!
=================
最有意义的也最值得LZ学习的。我想1楼的就是这个代码
da.Fill(ds, startRecord, PageItem, "a");
分页获取数据源方案
我用了这么长时间了一点问题都没有的
你好好看 你的什么地方的代码还有问题你加这个了吗
需要在前台添加一个 <div id="PageInfo" runat="server" >