原帖:http://topic.csdn.net/u/20091014/13/27c7a727-cdf7-4b02-9853-a60ec8b35abb.html?seed=896598991&r=60407787#r_60407787就是在点击搜索之后再点击分页就没有数据了 private static string _keyWords = "1=1";
private static int countNum = 0; protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindUersList(1);
}
}
#region 存储过程绑定[用户列表]
public void BindUersList(int currentPageNo)
{
countNum = (int)SqlHelper.GetExecuteScalar(CommandType.Text, "Select Count(ID) From Users", null);
//if (!string.IsNullOrEmpty(Request.Form["Select1"]))
//{
// _keyWords = " ProvinceID='" + Request.Form["Select1"].ToString().Trim() + "'";
// countNum = (int)SqlHelper.GetExecuteScalar(CommandType.Text, "Select Count(ID) From Users Where " + _keyWords + "", null);
//}
if (!string.IsNullOrEmpty(TextBox1.Text))
{
_keyWords += " And RealName like '%" + TextBox1.Text.Trim() + "%' Or MemberID like '%" + TextBox1.Text.Trim() + "%' Or Mobile like '%" + TextBox1.Text.Trim() + "%' Or Phone like '%" + TextBox1.Text.Trim() + "%'";
countNum = (int)SqlHelper.GetExecuteScalar(CommandType.Text, "Select Count(ID) From Users Where " + _keyWords + "", null);
} pager1.ItemCount = countNum;
GridView1.DataSource = (new QinMi_Pagination()).GetAllInfoPage("Users", "ID", currentPageNo, pager1.PageSize, "*", "RegTime Desc", _keyWords, 0);
GridView1.DataBind();
}
#endregion #region 搜索->按钮事件
protected void Button1_Click(object sender, EventArgs e)
{
countNum = (int)SqlHelper.GetExecuteScalar(CommandType.Text, "Select Count(ID) From Users", null);
//if (!string.IsNullOrEmpty(Request.Form["Select1"]))
//{
// _keyWords = " ProvinceID='" + Request.Form["Select1"].ToString().Trim() + "'";
// countNum = (int)SqlHelper.GetExecuteScalar(CommandType.Text, "Select Count(ID) From Users Where " + _keyWords + "", null);
//}
if (!string.IsNullOrEmpty(TextBox1.Text))
{
_keyWords += " And RealName like '%" + TextBox1.Text.Trim() + "%' Or MemberID like '%" + TextBox1.Text.Trim() + "%' Or Mobile like '%" + TextBox1.Text.Trim() + "%' Or Phone like '%" + TextBox1.Text.Trim() + "%'";
countNum = (int)SqlHelper.GetExecuteScalar(CommandType.Text, "Select Count(ID) From Users Where " + _keyWords + "", null);
}
pager1.CurrentIndex = 1;
pager1.ItemCount = countNum;
GridView1.DataSource = (new QinMi_Pagination()).GetAllInfoPage("Users", "ID", 1, pager1.PageSize, "*", "RegTime Desc", _keyWords, 0);
GridView1.DataBind();
}
#endregion #region 分页->存储过程点击分页
public void pager_Command(object sender, CommandEventArgs e)
{
int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
pager1.CurrentIndex = currnetPageIndx;
BindUersList(currnetPageIndx);
}
#endregion
private static int countNum = 0; protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindUersList(1);
}
}
#region 存储过程绑定[用户列表]
public void BindUersList(int currentPageNo)
{
countNum = (int)SqlHelper.GetExecuteScalar(CommandType.Text, "Select Count(ID) From Users", null);
//if (!string.IsNullOrEmpty(Request.Form["Select1"]))
//{
// _keyWords = " ProvinceID='" + Request.Form["Select1"].ToString().Trim() + "'";
// countNum = (int)SqlHelper.GetExecuteScalar(CommandType.Text, "Select Count(ID) From Users Where " + _keyWords + "", null);
//}
if (!string.IsNullOrEmpty(TextBox1.Text))
{
_keyWords += " And RealName like '%" + TextBox1.Text.Trim() + "%' Or MemberID like '%" + TextBox1.Text.Trim() + "%' Or Mobile like '%" + TextBox1.Text.Trim() + "%' Or Phone like '%" + TextBox1.Text.Trim() + "%'";
countNum = (int)SqlHelper.GetExecuteScalar(CommandType.Text, "Select Count(ID) From Users Where " + _keyWords + "", null);
} pager1.ItemCount = countNum;
GridView1.DataSource = (new QinMi_Pagination()).GetAllInfoPage("Users", "ID", currentPageNo, pager1.PageSize, "*", "RegTime Desc", _keyWords, 0);
GridView1.DataBind();
}
#endregion #region 搜索->按钮事件
protected void Button1_Click(object sender, EventArgs e)
{
countNum = (int)SqlHelper.GetExecuteScalar(CommandType.Text, "Select Count(ID) From Users", null);
//if (!string.IsNullOrEmpty(Request.Form["Select1"]))
//{
// _keyWords = " ProvinceID='" + Request.Form["Select1"].ToString().Trim() + "'";
// countNum = (int)SqlHelper.GetExecuteScalar(CommandType.Text, "Select Count(ID) From Users Where " + _keyWords + "", null);
//}
if (!string.IsNullOrEmpty(TextBox1.Text))
{
_keyWords += " And RealName like '%" + TextBox1.Text.Trim() + "%' Or MemberID like '%" + TextBox1.Text.Trim() + "%' Or Mobile like '%" + TextBox1.Text.Trim() + "%' Or Phone like '%" + TextBox1.Text.Trim() + "%'";
countNum = (int)SqlHelper.GetExecuteScalar(CommandType.Text, "Select Count(ID) From Users Where " + _keyWords + "", null);
}
pager1.CurrentIndex = 1;
pager1.ItemCount = countNum;
GridView1.DataSource = (new QinMi_Pagination()).GetAllInfoPage("Users", "ID", 1, pager1.PageSize, "*", "RegTime Desc", _keyWords, 0);
GridView1.DataBind();
}
#endregion #region 分页->存储过程点击分页
public void pager_Command(object sender, CommandEventArgs e)
{
int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
pager1.CurrentIndex = currnetPageIndx;
BindUersList(currnetPageIndx);
}
#endregion
_keyWords += " And RealName like '%" + TextBox1.Text.Trim() + "%' Or MemberID like '%" + TextBox1.Text.Trim() + "%' Or Mobile like '%" + TextBox1.Text.Trim() + "%' Or Phone like '%" + TextBox1.Text.Trim() + "%'";
countNum = (int)SqlHelper.GetExecuteScalar(CommandType.Text, "Select Count(ID) From Users Where " + _keyWords + "", null);countNum 是所有记录,经过keyWords 查询后的记录数一般没有这么多。
就是数,显示的记录数是所有记录的总数,但实际查询出来的记录数小于等于总记录数,你点击分页的页码,很可能没有这么多记录,所以查询没有数据。
public void pager_Command(object sender, CommandEventArgs e)
{
int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
pager1.CurrentIndex = currnetPageIndx;
BindUersList(currnetPageIndx);
}
了?这段代码不是运行搜索的呀?
public void pager_Command(object sender, CommandEventArgs e)
{
int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
pager1.CurrentIndex = currnetPageIndx;
BindUersList(currnetPageIndx);
}
这里的currnetPageIndx 是多少?
0 没有看到你 QinMi_Pagination类是怎样用 pager1 的内容,你该不会是用表态变量吧???
_keyWords 你用了静态变量,在多用户访问时,_keyWords的值就出现了多线程间的冲突,也就说,另一个请求会改变你的_keyWords值1 你大量用到 like ‘%xx%’ 这个性能很差的
2 countNum 你执行了2次查询,影响性能,这样先判断会好些
if (!string.IsNullOrEmpty(TextBox1.Text))
{
_keyWords += " And RealName like '%" + TextBox1.Text.Trim() + "%' Or MemberID like '%" + TextBox1.Text.Trim() + "%' Or Mobile like '%" + TextBox1.Text.Trim() + "%' Or Phone like '%" + TextBox1.Text.Trim() + "%'";
countNum = (int)SqlHelper.GetExecuteScalar(CommandType.Text, "Select Count(ID) From Users Where " + _keyWords + "", null);
}
else
{
countNum = (int)SqlHelper.GetExecuteScalar(CommandType.Text, "Select Count(ID) From Users", null);
}
private static string _keyWords = "1=1";
private static int countNum = 0;改为成员变量吧
因为你 _keyWords += "xxx"第1次访问时,
_keyWords = "1=1";
执行后,变成 _keyWords = "1=1 and RealName like xxx Or MemberID like xxx";
第2次时执行后,变成 _keyWords = "1=1 and RealName like xxx Or MemberID like xxx and RealName like xxx Or MemberID like xxx";
会这样不断加下去