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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
showData(1, 4);//显示第一页记录。。每页显示四条记录
this.btnPerPage.Enabled = false;
this.btnFirstPage.Enabled = false;
getCountPage(4);//窗本加载时得到总页数
FillDropPage();//添充下拉框
}
}
DataBase dbs = new DataBase();
protected static int pageNow = 1;//当前页数
protected static int CountPage;//总页数
protected void FillDropPage()
{
for (int i = 1; i <=CountPage; i++)
{
this.DropPage.Items.Add(i.ToString());
}
}
protected void getCountPage(int i_page)//得到总页数
{
int Count = dbs.ExecuteScalar(CommandType.Text, "select count(*) from Test", null);
CountPage = Count / i_page;
if (Count % i_page > 0)
{
++CountPage;
}
if (CountPage== 1)
{
this.btnLastPage.Enabled = false;
this.btnNextPage.Enabled = false;
}
}
//显示数据
protected void showData(int i_pageNow, int i_pageSize)
{
//得到总记录数
int currentPageStar = (i_pageNow - 1) * i_pageSize + 1;//当前页的第一条记录
int currentPageEnd = i_pageNow * i_pageSize;//当前页的最后一条记录
SqlParameter[] Params = new SqlParameter[2];
Params[0] = dbs.MakeInParams("@currentPageStar", SqlDbType.VarChar, 4, currentPageStar);
Params[1] = dbs.MakeInParams("@currentPageEnd", SqlDbType.VarChar, 4, currentPageEnd);
DataSet dst = new DataSet();
dst = dbs.GetDst(CommandType.StoredProcedure, "p_GetData", Params);
this.GridView1.DataSource = dst.Tables[0].DefaultView;
this.GridView1.DataBind();
}
//上一页按钮事件
protected void btnPerPage_Click1(object sender, EventArgs e)
{
pageNow = pageNow - 1;
this.DropPage.Text = pageNow.ToString();
showData(pageNow, 4);
this.btnNextPage.Enabled = true;
this.btnLastPage.Enabled = true;
if (pageNow == 1)
{
this.btnPerPage.Enabled = false;
this.btnFirstPage.Enabled = false;
}
}
//下一页按钮事件
protected void btnNextPage_Click1(object sender, EventArgs e)
{
pageNow = pageNow + 1;
this.DropPage.Text = pageNow.ToString();
showData(pageNow, 4);
this.btnPerPage.Enabled = true;
this.btnFirstPage.Enabled = true;
if (pageNow == CountPage)
{
this.btnNextPage.Enabled = false;
this.btnLastPage.Enabled = false;
}
}
//首页按钮事件
protected void btnFirstPage_Click(object sender, EventArgs e)
{
FisrtEvent();
}
//末页按钮事件
protected void btnLastPage_Click(object sender, EventArgs e)
{
LastEvent();
}
//通过下拉框选择装到第几页事件
protected void DropPage_SelectedIndexChanged(object sender, EventArgs e)
{
int selectPage = Convert.ToInt32(this.DropPage.SelectedItem.Text);
if (selectPage == 1)//选择了首页
{
FisrtEvent();
}
else if (selectPage == CountPage)//选择了末页
{
LastEvent();
}
else
{
pageNow =selectPage ;
showData(pageNow, 4);
}
}
////选择首页事件(被调用)
public void FisrtEvent()
{
this.btnPerPage.Enabled = false;
this.btnNextPage.Enabled = true;
this.btnLastPage.Enabled = true;
this.btnFirstPage.Enabled = false;
pageNow = 1;
this.DropPage.Text = pageNow.ToString();
showData(1, 4);
}
////选择末页事件(被调用)
public void LastEvent()
{
this.btnNextPage.Enabled = false;
this.btnPerPage.Enabled = true;
this.btnFirstPage.Enabled = true;
this.btnLastPage.Enabled = false;
pageNow = CountPage;
this.DropPage.Text = pageNow.ToString();
showData(CountPage, 4);
}
}
PS:大家在看showDate()事件时会发现我在配置参数时调用了基类,
还有就是读取表中第m到n条数据的存储过程都没有写出来。。。。
读取第m条到n条数据在我的博客中有。大家可以去看看: 用sql语句查询从N条到M条的记录
还有就是我想
将这一大串代码封装一下。。若能做成一个组件那就更好了。。希望各位大虾
指点。。。。谢谢。。。