aspx文件代码
<div>
ID 姓名 年龄<br /><br />
<asp:Repeater ID="Repeater1" runat="server"
OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<%# Eval("id") %> <%# Eval("name") %> <%# Eval("age") %><br />
</ItemTemplate>
</asp:Repeater>
<br />
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">首页</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton3_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" OnClick="LinkButton4_Click">尾页</asp:LinkButton>
当前第<asp:Label ID="Label1" runat="server"></asp:Label>页
共<asp:Label ID="Label2" runat="server"></asp:Label>页
</div>
aspx.cs文件 后台代码
namespace XinYuNews.news
{
public partial class fen : System.Web.UI.Page
{
public static int pageSize = 2;//每页显示的条数
public static int pageCurrent = 1;//起始页
public static int pageAll;//总页数
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
if ((Toatal() % pageSize) == 0)
{
pageAll = Toatal() / pageSize;
}
else
{
pageAll = Toatal() / pageSize + 1;
}
this.Label2.Text = pageAll.ToString();
this.Label1.Text = pageCurrent.ToString();
BindData(pageSize, pageCurrent);
}
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
}
//首页
protected void LinkButton1_Click(object sender, EventArgs e)
{
pageCurrent = 1;
this.Label1.Text = "1";
BindData(pageSize, pageCurrent);
this.LinkButton2.Enabled = false;
this.LinkButton3.Enabled = true;
}
//上一页
protected void LinkButton2_Click(object sender, EventArgs e)
{
pageCurrent--;
this.Label1.Text = pageCurrent.ToString();
this.LinkButton3.Enabled = true;
BindData(pageSize, pageCurrent);
if(pageCurrent == 1)
{
this.LinkButton2.Enabled = false;
}
}
//下一页
protected void LinkButton3_Click(object sender, EventArgs e)
{
pageCurrent++;
this.Label1.Text = pageCurrent.ToString();
this.LinkButton2.Enabled = true;
//this.LinkButton1.Enabled = true;
BindData(pageSize, pageCurrent);
if(pageCurrent >= pageAll)
{
this.LinkButton3.Enabled = false;
}
}
//尾页
protected void LinkButton4_Click(object sender, EventArgs e)
{
pageCurrent = pageAll;
this.Label1.Text = pageCurrent.ToString();
BindData(pageSize, pageAll);
this.LinkButton3.Enabled = false;
this.LinkButton2.Enabled = true;
}
public DataTable BindData(int pageSize, int pageCurrent)
{
SqlConnection conn = new SqlConnection("Data Source=3BVUYTU2L975TI7;Initial Catalog=NewsXin;User ID=son;Password=123;");
conn.Open();
string sql = "select top " + pageSize + " * from student where id not in (select top " + pageSize * (pageCurrent - 1) + " id from student)";
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataAdapter dr = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
dr.Fill(ds,"student");
DataTable dt = ds.Tables[0];
this.Repeater1.DataSource = dt;
this.Repeater1.DataBind();
return dt;
}
public int Toatal()
{
SqlConnection conn = new SqlConnection("Data Source=3BVUYTU2L975TI7;Initial Catalog=NewsXin;User ID=son;Password=123;");
conn.Open();
string sql = "select count(*) from student";
SqlCommand cmd = new SqlCommand(sql, conn);
int page = (int)cmd.ExecuteScalar();
conn.Close();
return page;
}
}
}