一 传统分页显示程序(datagrid )
private void PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataSet ds1 = (DataSet) Cache["ECInforDataSet"];
DataGrid dg = (DataGrid)source;
dg.DataSource = ds1.Tables[0].DefaultView;
dg.CurrentPageIndex = e.NewPageIndex;
this.DataBind();
}
其中Cache["ECInforDataSet"]如下设置:
SqlProcedure sp1 = new SqlProcedure("SelectName2");
sp1.m_ConnStr="Data Source=localhost;Initial Catalog=hangwu;persist security info=True;user id=sa;password=sa;";
SqlResult rst1 = sp1.Call(txtSanZiMa.Text.ToString(),txtYuanGongHao.Text.ToString());
//Response.Write(txtSanZiMa.Text.ToString());
Cache["ECInforDataSet"] = rst1.dataSet;
//备注——感觉使用Cache有点象使用Session
二 高效分页技术——每次只读几页——而不是一下将全部数据读进来
其中利用的分页的存储过程见四的8
//页的大小
private static int PageSize = 5;
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
//显示第一页的记录
ShowResult(0, PageSize);
}
}
void ShowResult(int pageIndex, int pageSize)
{
//绑定Repeater控件
products.DataSource = BLL.Product.GetProductsByCategory(int.Parse(Request.QueryString["categoryId"]),
pageSize, pageIndex);
products.DataBind();
//调用Product类中的方法获得该类商品的总数
int resultCount = BLL.Product.GetProductCountByCategory(int.Parse(Request.QueryString["categoryId"]));
int count;
//如果查询结果总数是页大小的整数倍
if (resultCount%PageSize == 0)
{
count = resultCount/PageSize;
PageCount.Text = count.ToString();
}
else
{
count = resultCount/PageSize+1;
PageCount.Text = count.ToString();
}
page.Items.Clear();
//绑定页码到DropDownList控件
for(int i=0; i<count; i++)
{
ListItem item = new ListItem((i+1).ToString(), i.ToString());
page.Items.Add(item);
}
page.SelectedIndex = pageIndex;
}
//分页显示中用到的页面切换——这里用到的DropDownList控件
private void page_SelectedIndexChanged(object sender, System.EventArgs e)
{
ShowResult(page.SelectedIndex, PageSize);
}
//其中用到的BLL.Product.GetProductsByCategory(int.Parse(Request.QueryString["categoryId"]),pageSize, pageIndex);的在如下位置定义:
BLL中的Product.cs中定义:
public static SqlDataReader GetProductsByCategory(int categoryId, int pageSize, int pageIndex)
{
SqlParameter[] para = {
new SqlParameter("@CategoryId", categoryId),
new SqlParameter("@pageSize", pageSize),
new SqlParameter("@pageIndex", pageIndex)
};
return SQLHelper.ExecuteReader(SQLHelper.CONN_STRING, CommandType.StoredProcedure, "GetProductByCategory", para);
}