使用Sql语句实现AspNetPager分页的范例
上一篇中介绍如何使用AspNetPager+DataGrid做分页。 这一篇是介绍使用Sql语句来做分页
其实最重要的问题就在
Select Top {0} * From User Where user_id Not in ( Select Top {1} user_id From User Order By user_id
使用sql本身已经把要分页的数据取出来。。其实DataGrid只是绑定显示而已。AspNetPager只是做了显示页码等数据显示而已
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack )
{
//求该数据集合的记录总和
SqlDataBase SqlDB=new SqlDataBase(DSN);
string strsql="Select count(user_id) from User";
DataSet ds= SqlDB.CreateSet(strsql,"IUser");
this.AspNetPager1.RecordCount=System.Convert.ToInt32(ds.Tables[0].Rows[0][0]) ;
SqlDB.CloseConnection();
BindData();
}
}
void BindData()
{
SqlDataBase SqlDB=new SqlDataBase(DSN);
int RecordPage=(AspNetPager1.CurrentPageIndex-1)*AspNetPager1.PageSize;
string strsql=String.Format(" Select Top {0} * From User Where user_id Not in ( Select Top {1} user_id From User Order By user_id)",AspNetPager1.PageSize.ToString(),RecordPage.ToString());
SqlDataAdapter adapter= SqlDB.CreateAdapter(strsql);
DataSet ds=new DataSet();
ds=SqlDB.CreateSet(strsql,"IUser");
//adapter.Fill(ds,AspNetPager1.PageSize*(AspNetPager1.CurrentPageIndex-1),AspNetPager1.PageSize,"IU_User");
this.DataGrid1.DataSource=ds.Tables["IUser"];
this.DataGrid1.DataBind();
//动态设置用户自定义文本内容
AspNetPager1.CustomInfoText="记录总数:<font color=\"blue\"><b>"+AspNetPager1.RecordCount.ToString()+"</b></font>";
AspNetPager1.CustomInfoText+=" 总页数:<font color=\"blue\"><b>"+AspNetPager1.PageCount.ToString()+"</b></font>";
AspNetPager1.CustomInfoText+=" 当前页:<font color=\"red\"><b>"+AspNetPager1.CurrentPageIndex.ToString()+"</b></font>";
}
private void AspNetPager1_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
{
AspNetPager1.CurrentPageIndex=e.NewPageIndex;
BindData();
System.Text.StringBuilder sb=new StringBuilder("<script Language=\"Javascript\"><!--\n");
sb.Append("var el=document.all;");
sb.Append(this.DataGrid1.ClientID);
sb.Append(".scrollIntoView(true);");
sb.Append("<");
sb.Append("/");
sb.Append("script>");
if(!Page.IsStartupScriptRegistered("scrollScript"))
Page.RegisterStartupScript("scrollScript",sb.ToString());
}
其实最重要的问题就在
Select Top {0} * From User Where user_id Not in ( Select Top {1} user_id From User Order By user_id
使用sql本身已经把要分页的数据取出来。。其实DataGrid只是绑定显示而已。AspNetPager只是做了显示页码等数据显示而已
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack )
{
//求该数据集合的记录总和
SqlDataBase SqlDB=new SqlDataBase(DSN);
string strsql="Select count(user_id) from User";
DataSet ds= SqlDB.CreateSet(strsql,"IUser");
this.AspNetPager1.RecordCount=System.Convert.ToInt32(ds.Tables[0].Rows[0][0]) ;
SqlDB.CloseConnection();
BindData();
}
}
void BindData()
{
SqlDataBase SqlDB=new SqlDataBase(DSN);
int RecordPage=(AspNetPager1.CurrentPageIndex-1)*AspNetPager1.PageSize;
string strsql=String.Format(" Select Top {0} * From User Where user_id Not in ( Select Top {1} user_id From User Order By user_id)",AspNetPager1.PageSize.ToString(),RecordPage.ToString());
SqlDataAdapter adapter= SqlDB.CreateAdapter(strsql);
DataSet ds=new DataSet();
ds=SqlDB.CreateSet(strsql,"IUser");
//adapter.Fill(ds,AspNetPager1.PageSize*(AspNetPager1.CurrentPageIndex-1),AspNetPager1.PageSize,"IU_User");
this.DataGrid1.DataSource=ds.Tables["IUser"];
this.DataGrid1.DataBind();
//动态设置用户自定义文本内容
AspNetPager1.CustomInfoText="记录总数:<font color=\"blue\"><b>"+AspNetPager1.RecordCount.ToString()+"</b></font>";
AspNetPager1.CustomInfoText+=" 总页数:<font color=\"blue\"><b>"+AspNetPager1.PageCount.ToString()+"</b></font>";
AspNetPager1.CustomInfoText+=" 当前页:<font color=\"red\"><b>"+AspNetPager1.CurrentPageIndex.ToString()+"</b></font>";
}
private void AspNetPager1_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
{
AspNetPager1.CurrentPageIndex=e.NewPageIndex;
BindData();
System.Text.StringBuilder sb=new StringBuilder("<script Language=\"Javascript\"><!--\n");
sb.Append("var el=document.all;");
sb.Append(this.DataGrid1.ClientID);
sb.Append(".scrollIntoView(true);");
sb.Append("<");
sb.Append("/");
sb.Append("script>");
if(!Page.IsStartupScriptRegistered("scrollScript"))
Page.RegisterStartupScript("scrollScript",sb.ToString());
}