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(); }}
{
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(); }}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货