读书人

AspNetPager控件干翻页从其他页返回

发布时间: 2013-06-25 23:45: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();



}

}


[解决办法]
把条件以url(GET)方式发送回来,包括page

读书人网 >asp.net

热点推荐