需求是先在页面上用GridView1显示数据库所有字段,用AspNetPager1控件分页,然后页面上有个按钮和一个文本框,在文本框里输入姓,点击按钮查询,在GridView里显示出模糊查询出来这个姓的所有人的信息!信息依然显示在原来的GridView1里,分页还是AspNetPager1分页
UserDB这是DB类里的两个查询方法
public DataTable InsertUsern()
{
DataTable ds = new DataTable();
string sql = "select id,username,sex,age,email,shengri from UserInfo";
SqlConnection con = new SqlConnection(url);
try
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
sda.Fill(ds);
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
con.Close();
}
return ds;
}
public DataTable InsertN(String name)
{
DataTable table = new DataTable();
string sql = "select id,username,sex,age,email,shengri from UserInfo where username like '"+name+"%%'";
SqlConnection con = new SqlConnection(url);
try
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
sda.Fill(table);
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
con.Close();
}
return table;
}这是页面代码:
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{ Label1.Text = Session["name"] as string;
BinData();
}
}
protected void BinData()
{
UserDB db = new UserDB();
DataTable table = db.InsertUsern();
PagedDataSource pds = new PagedDataSource();
pds.DataSource = table.DefaultView;
pds.AllowPaging = true;
AspNetPager1.RecordCount = pds.DataSourceCount;
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
pds.PageSize = AspNetPager1.PageSize;
this.GridView1.DataSource = pds;
this.GridView1.DataBind();
}
protected void BinData1()
{
UserDB db = new UserDB();
DataTable table = db.InsertN(TextBox1.Text);
PagedDataSource pds = new PagedDataSource();
pds.DataSource = table.DefaultView;
pds.AllowPaging = true;
AspNetPager1.RecordCount = pds.DataSourceCount;
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
pds.PageSize = AspNetPager1.PageSize;
this.GridView1.DataSource = pds;
this.GridView1.DataBind();
}
protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
BinData();
BinData1();
}
}
//查询按钮
protected void Button1_Click(object sender, EventArgs e)
{
BinData1(); }
我是这样写的,所有信息能够显示出来,可以分页!但是有个问题,我在文本框里输入了姓,没点击按钮查询,然后点击下一页,发现居然显示的是查询数据
怎么改啊。请问?找个很多方法,说是用viewstate记录查询条件,但是我没接触过这个,具体怎么用,请各位指教指教
UserDB这是DB类里的两个查询方法
public DataTable InsertUsern()
{
DataTable ds = new DataTable();
string sql = "select id,username,sex,age,email,shengri from UserInfo";
SqlConnection con = new SqlConnection(url);
try
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
sda.Fill(ds);
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
con.Close();
}
return ds;
}
public DataTable InsertN(String name)
{
DataTable table = new DataTable();
string sql = "select id,username,sex,age,email,shengri from UserInfo where username like '"+name+"%%'";
SqlConnection con = new SqlConnection(url);
try
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
sda.Fill(table);
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
con.Close();
}
return table;
}这是页面代码:
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{ Label1.Text = Session["name"] as string;
BinData();
}
}
protected void BinData()
{
UserDB db = new UserDB();
DataTable table = db.InsertUsern();
PagedDataSource pds = new PagedDataSource();
pds.DataSource = table.DefaultView;
pds.AllowPaging = true;
AspNetPager1.RecordCount = pds.DataSourceCount;
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
pds.PageSize = AspNetPager1.PageSize;
this.GridView1.DataSource = pds;
this.GridView1.DataBind();
}
protected void BinData1()
{
UserDB db = new UserDB();
DataTable table = db.InsertN(TextBox1.Text);
PagedDataSource pds = new PagedDataSource();
pds.DataSource = table.DefaultView;
pds.AllowPaging = true;
AspNetPager1.RecordCount = pds.DataSourceCount;
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
pds.PageSize = AspNetPager1.PageSize;
this.GridView1.DataSource = pds;
this.GridView1.DataBind();
}
protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
BinData();
BinData1();
}
}
//查询按钮
protected void Button1_Click(object sender, EventArgs e)
{
BinData1(); }
我是这样写的,所有信息能够显示出来,可以分页!但是有个问题,我在文本框里输入了姓,没点击按钮查询,然后点击下一页,发现居然显示的是查询数据
怎么改啊。请问?找个很多方法,说是用viewstate记录查询条件,但是我没接触过这个,具体怎么用,请各位指教指教
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
BinData();
BinData1();
}实际上你已经BinData1()查询了。
{
UserDB db = new UserDB();
DataTable table;
if (!string.IsNullOrEmpty(TextBox1.Text))//文本框有zhi就执行模糊查询。否则执行全部查询
{
table = db.InsertN(TextBox1.Text);
}
else
{
table = db.InsertUsern();
}
PagedDataSource pds = new PagedDataSource();
pds.DataSource = table.DefaultView;
pds.AllowPaging = true;
AspNetPager1.RecordCount = pds.DataSourceCount;
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
pds.PageSize = AspNetPager1.PageSize;
this.GridView1.DataSource = pds;
this.GridView1.DataBind();
}
protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
BinData();
}
{
table = db.InsertN(TextBox1.Text);
}
else
{
table = db.InsertUsern();
}
table做了判断,但是AspNetPager1_PageChanging里并没有做判断啊,如果在TEXTBOX里输入了值,我并没有点查询,但是里面确实有值了,所以点下一页还是自动会查询
{
string s = (string)ViewState[Key];
bind(s);
}
protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
BinData();
if(TextBox1.Text!=""){
BinData1();
}
}
{
get { return ViewState["Key"] == null ? "" : Convert.ToInt32(ViewState["Key"].ToString()); }
set { ViewState["Key"] = value; }
}
if (!string.IsNullOrEmpty(TextBox1.Text))//文本框有zhi就执行模糊查询。否则执行全部查询
{
key=TextBox1.Text;
}
table = db.InsertN(key); 当key为空,查询所有(可以调用InsertUsern),不为空则根据key查询数据
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
bind1();
}
这样导致的结果是AspNetPager1不受控制,textbox1只要输入了值,没有查询,下一页还是会弹出查询的结果其实关键是AspNetPager1_PageChanging里记录查询,关键是这个怎么写!
绑定BinData1();去掉pds设置为全局变量点击的时候赋值demo
BinData() 函数里加一句 ViewState["search"] = 1; //保存查询条件为1BinData1() 函数里加一句 ViewState["search"] = 2; //保存查询条件为2AspNetPager1_PageChanging加个判断 if ((int)ViewState["search"] == 1)
{
BinData();
}
else
{
BinData1();
}
把下面这段代码放到查询中去,只有查询过了之后,key才会有值,否则,一直为空,当你清空文本框值,再查询,这个key为空,即查询所有
if (!string.IsNullOrEmpty(TextBox1.Text))//文本框有zhi就执行模糊查询。否则执行全部查询
{
key=TextBox1.Text;
}
else
{
key="";
}
tringBuilder strSql=new StringBuilder();
strSql.Append("你的sql语句");
。。
你的条件就是
if(填条件的那个框是否为空)
{
strSql.Append("sql语句");
}
条件有多少就写多少!。。
整条Sql语句就是strSql.ToString();
就这样不就OK了!