前台页面:
<asp:DataList ID="DataList1" runat="server" Width="600px">
<ItemTemplate>
<table id="tabNewInfo">
<tr>
<td><span>发布时间:</span><%#Eval("post_time") %></td>
<td></td>
<td><%#Eval("post_admin") %></td>
</tr>
<tr>
<td><span>标题:</span><%#Eval("title") %></td>
<td><span>作者:</span><%#Eval("writer") %></td>
<td><span>来源:</span><%#Eval("new_source") %></td>
</tr>
</table>
<table id="tabNewContent">
<tr>
<td>
<%#Eval("new_contect") %>
</td>
</tr>
</table>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:DataList>
<br />
总共<asp:Label ID="lblRecordCount" runat="server" Text="Label"></asp:Label>条记录
当前是第<asp:Label ID="lblCurrentPage" runat="server" Text="Label"></asp:Label>页
总共有<asp:Label ID="lblPageCount" runat="server" Text="Label"></asp:Label>页
<br />
<asp:LinkButton ID="lbnPrevPage" CommandName="Prev" runat="server" OnCommand="Page_OnClick">上一页</asp:LinkButton>
<asp:LinkButton ID="lbnNextPage" CommandName="Next" runat="server" OnCommand="Page_OnClick">下一页</asp:LinkButton>
</div>
cs文件代码:
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;
using System.IO;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
int PageSize, CurrentPage, PageCount, RecordCount;
SqlConnection conn;
protected void Page_Load(object sender, EventArgs e)
{
PageSize = 3;
conn = new SqlConnection("Data source=Aioros;Initial Catalog=GamsjoyDb;Persist Security Info=true;User ID=sa;Password=sa");
conn.Open();
if (!Page.IsPostBack)
{
ListBind();
CurrentPage = 0;
ViewState["PageIndex"] = 0;
RecordCount = CalculateRecord();
lblRecordCount.Text = RecordCount.ToString();
if (RecordCount % PageSize == 0)
{
PageCount = RecordCount / PageSize;
}
else
{
PageCount = RecordCount / PageSize + 1;
}
lblPageCount.Text = PageCount.ToString();
ViewState["PageCount"] = PageCount;
}
}
public int CalculateRecord() //返回总的记录行数
{
int intCount;
string strCount = "Select count(*) as icount from news";
SqlCommand MyComm = new SqlCommand(strCount, conn);
SqlDataReader dr = MyComm.ExecuteReader();
if (dr.Read())
{
intCount = Int32.Parse(dr["icount"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
}
ICollection CreateSource()//获取记录
{
int StartIndex;
StartIndex = CurrentPage * PageSize;
string strSel = "Select * from news orDER BY id DESC";
DataSet ds = new DataSet();
ds.Clear();
SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel, conn);
MyAdapter.Fill(ds, StartIndex, PageSize, "news");
return ds.Tables["news"].DefaultView;
}
public void ListBind()//绑定数据源到DataList控件
{
DataList1.DataSource = CreateSource();
DataList1.DataBind();
lbnNextPage.Visible = true;
lbnPrevPage.Visible = true;
if (CurrentPage == (PageCount - 1))
{
lbnNextPage.Visible = false;
}
if (CurrentPage == 0)
{
lbnPrevPage.Visible = false;
}
int x;
x = CurrentPage + 1;
lblCurrentPage.Text = x.ToString();
}
public 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++;
break;
case "Prev":
if (CurrentPage > 0)
CurrentPage--;
break;
}
ViewState["PageIndex"] = CurrentPage;
ListBind();
}
}
DataList控件实现分页
最新推荐文章于 2017-11-06 02:44:00 发布