怎么让DataList实现像csdn论坛一样的分页就事500篇 1 2 3 4 5 6 7 8 9 10 更多
解决方案 »
- 不显示删除回复显示所有回复显示星级回复显示得分回复 关于ASP.NET连接SQL2005的web.config配置问题,急,在线等[问题点数:100分,]
- 修改xml文件session丢失
- 关于使用RegularExpressionValidator验证FileUpload问题
- ViewState转换问题.ding者有分!~~~~~~
- 为何用RotateFlip旋转后的图象缩小了?
- DataGrid显示效果问题,请教高手了,急
- Request.Params()怎么接不到数据????
- 关于Excel保存的问题(高分相送)
- RegularExpressionValidator 之类的验证控件问题
- 非常小的问题, SQL语句的日期问题, ASP.net C#与ACCESS, 散分了.50分.UP有分
- 关于visual studio中页面设计的问题
- 急急急呀!!!一个Imagebotton的问题
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
{
SqlConnection conn = new SqlConnection(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 top 500 * from ts1";
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 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 ="共"+PageCount+"篇"+ OutPut;
}
}
请注意颜色标注部分
需要在前台添加一个
PageCount<div id="PageInfo" runat="server" >
删除 div前的 PageCount
笔误 .....
{
OutPut += " <a href='" + PageUrl + "?page=" + (CurrentPage - 1) + "'>" + "上一页" + " </a>";
}
if (CurrentPage < PageCount)
{
OutPut += " <a href='" + PageUrl + "?page=" + (CurrentPage + 1) + "'>" + "下一页" + " </a>";
}
把表注颜色部分删除OK