读书人

AspNetPager控件做翻页当从其他页返

发布时间: 2013-06-19 10:26:41 作者: rapoo

AspNetPager控件做翻页,当从其他页返回到第一页时,出错,查询条件丢失,如何保存查询条件
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{


string 条件= " from info where 1=1";

if (Request.Form["id"] != "")
{........条件语句 }
else
{........条件语句 }
string sqlStr = "select count(distinct(id)) as all_num " + 条件+"";

if (Request.QueryString["page"] == null)
{
Session["sqlString"] = sqlString;
}
else
{
sqlString = Session["sqlString"].ToString();
}
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()
{
string 条件= " from info where 1=1";

if (Request.Form["id"] != "")
{........条件语句 }
else
{........条件语句 }
if (Request.QueryString["page"] == null)
{
Session["sqlString"] = sqlString;


}
else
{
sqlString = Session["sqlString"].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();



}

}


[解决办法]
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;bind();
}


[解决办法]

引用:
to u010948625
就是这个问题 该如何解决啊?

简单一点,这么写吧(下面是伪代码,仅供参考)。
Page_Load()
{
if(!IsPostBack)
{
Session["id"] = Request["id"];//将查询条件保存到session
//其他代码...
}
}

Bind()
{
string id = Session["id"].ToString();
//其他代码...
}

读书人网 >asp.net

热点推荐