按自己的想法写了个分页(我用的是Datagrid中自带的页码分页),请大家帮忙看看,希望多多提意见
当然还用到了ViewState来避免重复查询,这里就不贴出了下面是两个调用的存储过程
create procedure sp_fristGetInfo
as
select top 5 articleid,title from dt_article UNION
select articleid,null from dt_article where articleid not in
(select top 5 articleid from dt_article)
gocreate procedure sp_otherGetInfo
@front int
as
select top 5 articleid,title from dt_article where articleid>=@front UNION
select articleid,null from dt_article where articleid not in
(select top 5 articleid from dt_article where articleid>=@front)
go先定义一个全局变量public static DataTable dt;
Page_load中的代码
if(!Page.IsPostBack)
{
SqlConnection conn = DB.CreateConnection();
SqlDataAdapter cmd = new SqlDataAdapter("sp_fristGetInfo",conn);
cmd.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
cmd.Fill(ds,"articleid");
this.DataGrid1.DataSource = ds.Tables[0];
this.DataGrid1.DataBind();
dt = ds.Tables[0];
}//PageIndexChanged事件
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
binddata(e.NewPageIndex);
}//PageIndexChanged事件中调用的函数
private void binddata(int pageIndex)
{
int front = 0 ;
front = Convert.ToInt32(dt.Rows[pageIndex*5][0]); //获取该页将要显示的第一行的数据的id SqlConnection conn = DB.CreateConnection();
SqlDataAdapter cmd = new SqlDataAdapter("sp_otherGetInfo",conn);
cmd.SelectCommand.CommandType = CommandType.StoredProcedure;
cmd.SelectCommand.Parameters.Add("@front",front);
DataSet ds = new DataSet();
cmd.Fill(ds,"articleid");
dt = null;
dt = ds.Tables["articleid"];
this.DataGrid1.DataSource = dt;
this.DataGrid1.DataBind();
}
当然还用到了ViewState来避免重复查询,这里就不贴出了下面是两个调用的存储过程
create procedure sp_fristGetInfo
as
select top 5 articleid,title from dt_article UNION
select articleid,null from dt_article where articleid not in
(select top 5 articleid from dt_article)
gocreate procedure sp_otherGetInfo
@front int
as
select top 5 articleid,title from dt_article where articleid>=@front UNION
select articleid,null from dt_article where articleid not in
(select top 5 articleid from dt_article where articleid>=@front)
go先定义一个全局变量public static DataTable dt;
Page_load中的代码
if(!Page.IsPostBack)
{
SqlConnection conn = DB.CreateConnection();
SqlDataAdapter cmd = new SqlDataAdapter("sp_fristGetInfo",conn);
cmd.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
cmd.Fill(ds,"articleid");
this.DataGrid1.DataSource = ds.Tables[0];
this.DataGrid1.DataBind();
dt = ds.Tables[0];
}//PageIndexChanged事件
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
binddata(e.NewPageIndex);
}//PageIndexChanged事件中调用的函数
private void binddata(int pageIndex)
{
int front = 0 ;
front = Convert.ToInt32(dt.Rows[pageIndex*5][0]); //获取该页将要显示的第一行的数据的id SqlConnection conn = DB.CreateConnection();
SqlDataAdapter cmd = new SqlDataAdapter("sp_otherGetInfo",conn);
cmd.SelectCommand.CommandType = CommandType.StoredProcedure;
cmd.SelectCommand.Parameters.Add("@front",front);
DataSet ds = new DataSet();
cmd.Fill(ds,"articleid");
dt = null;
dt = ds.Tables["articleid"];
this.DataGrid1.DataSource = dt;
this.DataGrid1.DataBind();
}
int EndNum = (CruntPage(当前页数)-1) * Pagesize(每页显示数);
if (EndNum == 0)
{
sql = "select top {0} * from OrderReview order by OrderReviewID desc";
}
else
{
sql = "select top {0} * from OrderReview where OrderReviewID not in (select top {1} OrderReviewID from OrderReview order by OrderReviewID desc) order by OrderReviewID desc";
sql = string.Format(sql, Pagesize, EndNum);
}