没有分页存储过程 ,用这个AspNetPager出错,返回第一页查询条件丢失,该如何解决
没有分页存储过程 ,用这个AspNetPager出错,返回第一页查询条件丢失
错误没有 主要是回到第一页 Request.QueryString["page"] 为空
返回第一页查询条件丢失
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string 条件= " from info where 1=1";
if (Request.Form["id"] != "")
{........条件语句 }
else
{........条件语句 }
if (Request.QueryString["page"] == null)
{
Session["条件"] = 条件;
}
else
{
条件 = Session["条件"].ToString();
}
string sqlStr = "select count(distinct(id)) as all_num " + 条件 + "";
SqlConnection conn = new SqlConnection(strCon); ; //新建数据库连接对象,其中s是上面的连接字符串
conn.Open(); //打开与数据库的连接
SqlCommand cmd = new SqlCommand(sqlStr, conn);
AspNetPager1.AlwaysShow = true;
AspNetPager1.PageSize = 2;
AspNetPager1.RecordCount = (int)cmd.ExecuteScalar();
conn.Close();
bind();
}
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
bind();
}
//绑定
public void bind()
{
if (Request.QueryString["page"] == null)
{
Session["条件"] = 条件;
}
else
{
条件 = Session["条件"].ToString();
}
string sqlstr = "select * " + 条件+"";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "myds");
GridView1.DataSource = myds.Tables["myds"];
GridView1.DataKeyNames = new string[] { "id" };
//主键
GridView1.DataBind();
sqlcon.Close();
}
}
------解决方案--------------------
错误没有 主要是回到第一页 Request.QueryString["page"] 为空
返回第一页查询条件丢失
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string 条件= " from info where 1=1";
if (Request.Form["id"] != "")
{........条件语句 }
else
{........条件语句 }
if (Request.QueryString["page"] == null)
{
Session["条件"] = 条件;
}
else
{
条件 = Session["条件"].ToString();
}
string sqlStr = "select count(distinct(id)) as all_num " + 条件 + "";
SqlConnection conn = new SqlConnection(strCon); ; //新建数据库连接对象,其中s是上面的连接字符串
conn.Open(); //打开与数据库的连接
SqlCommand cmd = new SqlCommand(sqlStr, conn);
AspNetPager1.AlwaysShow = true;
AspNetPager1.PageSize = 2;
AspNetPager1.RecordCount = (int)cmd.ExecuteScalar();
conn.Close();
bind();
}
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
bind();
}
//绑定
public void bind()
{
if (Request.QueryString["page"] == null)
{
Session["条件"] = 条件;
}
else
{
条件 = Session["条件"].ToString();
}
string sqlstr = "select * " + 条件+"";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "myds");
GridView1.DataSource = myds.Tables["myds"];
GridView1.DataKeyNames = new string[] { "id" };
//主键
GridView1.DataBind();
sqlcon.Close();
}
}
------解决方案--------------------