如何使用AspNetPager分页控件对动态查询的结果进行分页
1个回答
展开全部
注意:
如果 设置UrlPaging 为True,后台用ViewState是保存不住查询条件的,如果非要用Url分页,可以用Session保存查询条件。
后台代码 :
const string vsKey = "searchCompany"; //ViewState key
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindCompany();//服务区绑定
if (ViewState[vsKey] != null)
{
if (ViewState[vsKey].ToString() != "")
{
dp_Cname.Value = ViewState[vsKey].ToString().Split('=')[1].ToString(); //赋值 cid 也就是服务区的名字
}
}
showresult(string .Empty); //显示信息
}
}
//显示信息
void showresult(string sWhere)
{
DataTable dt = new DataTable();
ZGF_CG_DAL.SqlHelper.FillDataTable("select * from Rank_company_total_price "+sWhere, dt);
#region 自定义一个表格样式 得到想要的数据源 dtcol
System.Data.DataTable dtcol = new System.Data.DataTable();
dtcol.Columns.Add("Cname");//服务区名称
dtcol.Columns.Add("Riqi");//日期
dtcol.Columns.Add("Total_price",typeof(decimal));//总金额
dtcol.Columns.Add("Pming");//排名
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow drcol = dtcol.NewRow();
drcol["Cname"] = ZGF_CG_DAL.SqlHelper.ExecuteScalar("select Cname from Company where id='" + dt.Rows[i]["Cid"].ToString() + "'").ToString(); //服务区名称
drcol["Riqi"] = dt.Rows[i]["Riqi"].ToString();//日期
drcol["Total_price"] = Convert.ToDecimal(dt.Rows[i]["Total_price"].ToString()).ToString("#,##0.00");//总金额
drcol["Pming"] = dt.Rows[i]["Rank_num"].ToString();//排名
dtcol.Rows.Add(drcol);
}
#endregion
#region 绑定分页控件
AspNetPager1.RecordCount = dtcol .Rows .Count;
PagedDataSource pds = new PagedDataSource();
pds.DataSource = dtcol.DefaultView;
pds.AllowPaging = true;
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
pds.PageSize = AspNetPager1.PageSize;
Re_AdminUser.DataSource = pds;
Re_AdminUser.DataBind();
#endregion
}
//改变编号 对应前台 <%# Convert.ToInt32(Container.ItemIndex)+1 +setp() %>
public int setp()
{
int p = Convert.ToInt32(AspNetPager1.CurrentPageIndex);
return p * 10 - 10; // 其中的10代表每页显示10条记录
}
//分页
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
showresult((string)ViewState[vsKey]);
}
#region 服务区绑定
public void BindCompany()
{
string sql = "select Id ,Cname from Company order by Cname";
DataTable dt = ZGF_CG_DAL.DBhelp.ExecuteDataTable(sql);
this.dp_Cname.DataSource = dt;
this.dp_Cname.DataValueField = "Id";
this.dp_Cname.DataTextField = "Cname";
this.dp_Cname.DataBind();
this.dp_Cname.Items.Insert(0, "-所有服务区-");
}
#endregion
//查询
protected void btn_ok_Click(object sender, EventArgs e)
{
AspNetPager1.CurrentPageIndex = 1;
string s = "";
if (dp_Cname.Value != "-所有服务区-")
{
s = " where Cid=" + dp_Cname.Value;
}
ViewState[vsKey] = s;
showresult(s);
}
如果 设置UrlPaging 为True,后台用ViewState是保存不住查询条件的,如果非要用Url分页,可以用Session保存查询条件。
后台代码 :
const string vsKey = "searchCompany"; //ViewState key
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindCompany();//服务区绑定
if (ViewState[vsKey] != null)
{
if (ViewState[vsKey].ToString() != "")
{
dp_Cname.Value = ViewState[vsKey].ToString().Split('=')[1].ToString(); //赋值 cid 也就是服务区的名字
}
}
showresult(string .Empty); //显示信息
}
}
//显示信息
void showresult(string sWhere)
{
DataTable dt = new DataTable();
ZGF_CG_DAL.SqlHelper.FillDataTable("select * from Rank_company_total_price "+sWhere, dt);
#region 自定义一个表格样式 得到想要的数据源 dtcol
System.Data.DataTable dtcol = new System.Data.DataTable();
dtcol.Columns.Add("Cname");//服务区名称
dtcol.Columns.Add("Riqi");//日期
dtcol.Columns.Add("Total_price",typeof(decimal));//总金额
dtcol.Columns.Add("Pming");//排名
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow drcol = dtcol.NewRow();
drcol["Cname"] = ZGF_CG_DAL.SqlHelper.ExecuteScalar("select Cname from Company where id='" + dt.Rows[i]["Cid"].ToString() + "'").ToString(); //服务区名称
drcol["Riqi"] = dt.Rows[i]["Riqi"].ToString();//日期
drcol["Total_price"] = Convert.ToDecimal(dt.Rows[i]["Total_price"].ToString()).ToString("#,##0.00");//总金额
drcol["Pming"] = dt.Rows[i]["Rank_num"].ToString();//排名
dtcol.Rows.Add(drcol);
}
#endregion
#region 绑定分页控件
AspNetPager1.RecordCount = dtcol .Rows .Count;
PagedDataSource pds = new PagedDataSource();
pds.DataSource = dtcol.DefaultView;
pds.AllowPaging = true;
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
pds.PageSize = AspNetPager1.PageSize;
Re_AdminUser.DataSource = pds;
Re_AdminUser.DataBind();
#endregion
}
//改变编号 对应前台 <%# Convert.ToInt32(Container.ItemIndex)+1 +setp() %>
public int setp()
{
int p = Convert.ToInt32(AspNetPager1.CurrentPageIndex);
return p * 10 - 10; // 其中的10代表每页显示10条记录
}
//分页
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
showresult((string)ViewState[vsKey]);
}
#region 服务区绑定
public void BindCompany()
{
string sql = "select Id ,Cname from Company order by Cname";
DataTable dt = ZGF_CG_DAL.DBhelp.ExecuteDataTable(sql);
this.dp_Cname.DataSource = dt;
this.dp_Cname.DataValueField = "Id";
this.dp_Cname.DataTextField = "Cname";
this.dp_Cname.DataBind();
this.dp_Cname.Items.Insert(0, "-所有服务区-");
}
#endregion
//查询
protected void btn_ok_Click(object sender, EventArgs e)
{
AspNetPager1.CurrentPageIndex = 1;
string s = "";
if (dp_Cname.Value != "-所有服务区-")
{
s = " where Cid=" + dp_Cname.Value;
}
ViewState[vsKey] = s;
showresult(s);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询