用起来挺顺心。调用方式大致如下:
程序代码
protected void dataBind() {
int page = 1;
if (Request.QueryString["page"] != null){
page = Convert.ToInt32(Request["page"]);
}
if (Request.Form["page"] != null){
page = Convert.ToInt32(Request.Form["page"]);
}
int myPageSize = 20;
SqlConnection conn = new SqlConnection(dbTool.myConnStr);
SqlCommand cmd = new SqlCommand("p_page", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Tables", SqlDbType.VarChar, 50);
cmd.Parameters["@Tables"].Value = "testTable";
cmd.Parameters.Add("@PrimaryKey", SqlDbType.VarChar, 50);
cmd.Parameters["@PrimaryKey"].Value = "id";
cmd.Parameters.Add("@Sort", SqlDbType.VarChar, 50);
cmd.Parameters["@Sort"].Value = "id asc";
cmd.Parameters.Add("@CurrentPage", SqlDbType.Int, 4);
cmd.Parameters["@CurrentPage"].Value = page;
cmd.Parameters.Add("@PageSize", SqlDbType.Int, 4);
cmd.Parameters["@PageSize"].Value = myPageSize;
cmd.Parameters.Add("@Fields", SqlDbType.VarChar, 50);
cmd.Parameters["@Fields"].Value = "id,username";
cmd.Parameters.Add("@Filter", SqlDbType.VarChar, 50);
cmd.Parameters["@Filter"].Value = "id>1000 and id<10000";
cmd.Parameters.Add("@Group", SqlDbType.VarChar, 50);
cmd.Parameters["@Group"].Value = "";
cmd.Parameters.Add("@TotalPage", SqlDbType.Int, 4);
cmd.Parameters["@TotalPage"].Direction = ParameterDirection.Output;
cmd.Parameters.Add("@TotalRecord", SqlDbType.Int, 4);
cmd.Parameters["@TotalRecord"].Direction = ParameterDirection.ReturnValue;
conn.Open();
//先取得返回值
cmd.ExecuteNonQuery();
int totalPage, totalRecord;
totalPage = Convert.ToInt32(cmd.Parameters["@TotalPage"].Value);
totalRecord = Convert.ToInt32(cmd.Parameters["@totalRecord"].Value);
//再取得返回记录集
SqlDataReader sdr = cmd.ExecuteReader();
GridView1.DataSource = sdr;
GridView1.DataBind();
cmd.Dispose();
conn.Dispose();
tools mytool = new tools(Response, Request);
/*
* 功能:PageLink显示分页导航
* totalCount:总记录数
* totalPage:总页数
* Page:页码
* PerPageSize:每页显示记录数
* GroupSizes:每组分页显示的页码数
* PageArgu:分页参数
* ShowGoto:是否显示跳转
*/
lblPageLink.Text = mytool.PageLink(totalRecord, totalPage, page, myPageSize, 3, "page",true);
mytool.Dispose();
}
int page = 1;
if (Request.QueryString["page"] != null){
page = Convert.ToInt32(Request["page"]);
}
if (Request.Form["page"] != null){
page = Convert.ToInt32(Request.Form["page"]);
}
int myPageSize = 20;
SqlConnection conn = new SqlConnection(dbTool.myConnStr);
SqlCommand cmd = new SqlCommand("p_page", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Tables", SqlDbType.VarChar, 50);
cmd.Parameters["@Tables"].Value = "testTable";
cmd.Parameters.Add("@PrimaryKey", SqlDbType.VarChar, 50);
cmd.Parameters["@PrimaryKey"].Value = "id";
cmd.Parameters.Add("@Sort", SqlDbType.VarChar, 50);
cmd.Parameters["@Sort"].Value = "id asc";
cmd.Parameters.Add("@CurrentPage", SqlDbType.Int, 4);
cmd.Parameters["@CurrentPage"].Value = page;
cmd.Parameters.Add("@PageSize", SqlDbType.Int, 4);
cmd.Parameters["@PageSize"].Value = myPageSize;
cmd.Parameters.Add("@Fields", SqlDbType.VarChar, 50);
cmd.Parameters["@Fields"].Value = "id,username";
cmd.Parameters.Add("@Filter", SqlDbType.VarChar, 50);
cmd.Parameters["@Filter"].Value = "id>1000 and id<10000";
cmd.Parameters.Add("@Group", SqlDbType.VarChar, 50);
cmd.Parameters["@Group"].Value = "";
cmd.Parameters.Add("@TotalPage", SqlDbType.Int, 4);
cmd.Parameters["@TotalPage"].Direction = ParameterDirection.Output;
cmd.Parameters.Add("@TotalRecord", SqlDbType.Int, 4);
cmd.Parameters["@TotalRecord"].Direction = ParameterDirection.ReturnValue;
conn.Open();
//先取得返回值
cmd.ExecuteNonQuery();
int totalPage, totalRecord;
totalPage = Convert.ToInt32(cmd.Parameters["@TotalPage"].Value);
totalRecord = Convert.ToInt32(cmd.Parameters["@totalRecord"].Value);
//再取得返回记录集
SqlDataReader sdr = cmd.ExecuteReader();
GridView1.DataSource = sdr;
GridView1.DataBind();
cmd.Dispose();
conn.Dispose();
tools mytool = new tools(Response, Request);
/*
* 功能:PageLink显示分页导航
* totalCount:总记录数
* totalPage:总页数
* Page:页码
* PerPageSize:每页显示记录数
* GroupSizes:每组分页显示的页码数
* PageArgu:分页参数
* ShowGoto:是否显示跳转
*/
lblPageLink.Text = mytool.PageLink(totalRecord, totalPage, page, myPageSize, 3, "page",true);
mytool.Dispose();
}