try
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from Commentary", conn);
DataSet ds = new DataSet();
da.Fill(ds, "Commentary");
conn.Close(); objpds = new PagedDataSource();
objpds.DataSource = ds.Tables[0].DefaultView;
objpds.AllowPaging = true;
objpds.PageSize = 8;
int curPage;
if (Request.QueryString["Page"] != null)
{
curPage = Int32.Parse(Request.QueryString["Page"]);
}
else
{
curPage = 1;
} objpds.CurrentPageIndex = curPage - 1;
lblCurrentPage.Text = curPage.ToString(); //多少条记录
int totalrows = ds.Tables[0].Rows.Count;
pageCount = totalrows / 8;
if (totalrows % 8 > 0)
{
pageCount = pageCount + 1;
}
lblRecordCount.Text = totalrows.ToString(); //共几页
lblPageCount.Text = objpds.PageCount.ToString();
if (!objpds.IsFirstPage)
{
lnPrevPage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(curPage - 1);
lnPrevPage.Enabled = true;
lnPrevPage.ForeColor = System.Drawing.Color.CornflowerBlue;
}
else
{
lnPrevPage.Enabled = false;
}
if (!objpds.IsLastPage)
{
lnNextPage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(curPage + 1);
lnNextPage.Enabled = true;
lnNextPage.ForeColor = System.Drawing.Color.CornflowerBlue;
}
else
{
lnNextPage.Enabled = false;
}
//数据绑定 DataList1.DataSource = objpds;
DataList1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
~~~~~~~~~~~~~~~~~~~~~
<table id="Table1" border="0" cellpadding="0" cellspacing="0" style="width: 580px"
align="center">
<tr>
<td align="center" style="width: 580px; height: 19px;">
<font size="2">当前第</font><asp:Label ID="lblCurrentPage" runat="server" Font-Size="10pt"></asp:Label>
<font size="2">页 共</font><asp:Label ID="lblPageCount" runat="server" Font-Size="10pt"></asp:Label><font
size="2">页 </font>
<asp:Label ID="lblRecordCount" runat="server" Font-Size="10pt"></asp:Label>
<font size="2">条记录
<asp:HyperLink ID="lnPrevPage" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="lnNextPage" runat="server">下一页</asp:HyperLink>
跳至<asp:TextBox ID="textbox2" runat="server" Font-Size="10pt" Width="19px"></asp:TextBox>页
<asp:Button ID="button1" runat="server" Text="GO" Font-Size="10pt" Width="25px" OnClick="button1_Click" />
</font>
</td>
</tr>
</table>
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from Commentary", conn);
DataSet ds = new DataSet();
da.Fill(ds, "Commentary");
conn.Close(); objpds = new PagedDataSource();
objpds.DataSource = ds.Tables[0].DefaultView;
objpds.AllowPaging = true;
objpds.PageSize = 8;
int curPage;
if (Request.QueryString["Page"] != null)
{
curPage = Int32.Parse(Request.QueryString["Page"]);
}
else
{
curPage = 1;
} objpds.CurrentPageIndex = curPage - 1;
lblCurrentPage.Text = curPage.ToString(); //多少条记录
int totalrows = ds.Tables[0].Rows.Count;
pageCount = totalrows / 8;
if (totalrows % 8 > 0)
{
pageCount = pageCount + 1;
}
lblRecordCount.Text = totalrows.ToString(); //共几页
lblPageCount.Text = objpds.PageCount.ToString();
if (!objpds.IsFirstPage)
{
lnPrevPage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(curPage - 1);
lnPrevPage.Enabled = true;
lnPrevPage.ForeColor = System.Drawing.Color.CornflowerBlue;
}
else
{
lnPrevPage.Enabled = false;
}
if (!objpds.IsLastPage)
{
lnNextPage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(curPage + 1);
lnNextPage.Enabled = true;
lnNextPage.ForeColor = System.Drawing.Color.CornflowerBlue;
}
else
{
lnNextPage.Enabled = false;
}
//数据绑定 DataList1.DataSource = objpds;
DataList1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
~~~~~~~~~~~~~~~~~~~~~
<table id="Table1" border="0" cellpadding="0" cellspacing="0" style="width: 580px"
align="center">
<tr>
<td align="center" style="width: 580px; height: 19px;">
<font size="2">当前第</font><asp:Label ID="lblCurrentPage" runat="server" Font-Size="10pt"></asp:Label>
<font size="2">页 共</font><asp:Label ID="lblPageCount" runat="server" Font-Size="10pt"></asp:Label><font
size="2">页 </font>
<asp:Label ID="lblRecordCount" runat="server" Font-Size="10pt"></asp:Label>
<font size="2">条记录
<asp:HyperLink ID="lnPrevPage" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="lnNextPage" runat="server">下一页</asp:HyperLink>
跳至<asp:TextBox ID="textbox2" runat="server" Font-Size="10pt" Width="19px"></asp:TextBox>页
<asp:Button ID="button1" runat="server" Text="GO" Font-Size="10pt" Width="25px" OnClick="button1_Click" />
</font>
</td>
</tr>
</table>
详细代码百度找下第一个关键字,很多
DataList自定义分页
using System;
using System.Data;
using System.Configuration;
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 System.Data.SqlClient;
class DataList分页
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillData();
}
}
static PagedDataSource Pds;
void FillData()
{
SqlConnection Conn = new SqlConnection("Data Source=192.168.1.30;database=Ticket_Info_Data;UID=sa;Password=sa;Persist Security Info=True;");
Conn.Open();
SqlDataAdapter Sda = new SqlDataAdapter("select * from DomesticFlight", Conn);
DataSet Ds = new DataSet();
Sda.Fill(Ds);
Pds = new PagedDataSource();
Pds.DataSource = Ds.Tables[0].DefaultView;
Pds.AllowPaging = true;
Pds.PageSize = 6;
this.DataList1.DataSource = Pds;
this.DataList1.DataBind();
Conn.Close();
}
//下一页
protected void LinkButton2_Click(object sender, EventArgs e)
{
Pds.CurrentPageIndex++;
this.DataList1.DataSource = Pds;
this.DataList1.DataBind();
}
//上一页
protected void LinkButton1_Click(object sender, EventArgs e)
{
Pds.CurrentPageIndex--;
this.DataList1.DataSource = Pds;
this.DataList1.DataBind();
}
}
不过这个分页也很容易写,关键不是代码要你理解代码是什么意思
数据源从哪里来,点下一页和上一页的时候为什么数据库显示会不一样写程序就是这样,首先你必须要有思想,有了思想和构想,代码逻辑自然就出来了
写起代码也会容易多,别人一看就知道你写的代码的意思一个好的程序员,不是自己写的代码有多高深,而是写的代码通俗易懂!