代码如下
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace eMeng.Exam.DataGridPaging11
{
/// <summary>
/// DataGridPaging 的摘要说明。
/// </summary>
public class DataGridPaging11 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
protected System.Web.UI.WebControls.Label lblPageCount;
protected System.Web.UI.WebControls.Label lblCurrentIndex;
protected System.Web.UI.WebControls.LinkButton btnFirst;
protected System.Web.UI.WebControls.LinkButton btnPrev;
protected System.Web.UI.WebControls.LinkButton btnNext;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.LinkButton btnLast;
string comstr; private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
btnFirst.Text = "最首页";
btnPrev.Text = "前一页";
btnNext.Text = "下一页";
btnLast.Text = "最后页";
comstr="select *from barcode";
OpenDatabase();
BindGrid();
}
private void OpenDatabase()
{
SqlConnection con=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConStr"]);
con.Open();
}
private void ShowStats()
{
lblCurrentIndex.Text = "第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页";
lblPageCount.Text = "总共 " + MyDataGrid.PageCount.ToString() + " 页";
} public void PagerButtonClick(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch(arg)
{
case "next":
if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
{
MyDataGrid.CurrentPageIndex += 1;
}
break;
case "prev":
if (MyDataGrid.CurrentPageIndex > 0)
{
MyDataGrid.CurrentPageIndex -= 1;
}
break;
case "last":
MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
break;
default:
MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg);
break;
}
BindGrid();
ShowStats();
}
public void BindGrid()
{
SqlConnection con=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConStr"]);
con.Open();
SqlCommand cmd=new SqlCommand(comstr,con);
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=cmd;
DataSet ds=new DataSet();
da.Fill(ds,"barcode");
this.MyDataGrid.DataSource=ds;
this.MyDataGrid.DataBind();
con.Close();
}
public void MyDataGrid_Page(object sender, DataGridPageChangedEventArgs e)
{
int startIndex ;
startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;
MyDataGrid.CurrentPageIndex = e.NewPageIndex;
BindGrid();
ShowStats();
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
} /// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
MyDataGrid.CurrentPageIndex=0;
comstr="select *from barcode where name LIKE'%"  + this.TextBox1.Text +  "%' and spec LIKE'%"+this.TextBox2.Text+"%' and price LIKE'%"+ this.TextBox3.Text +"%' and barcode LIKE'%"+ this.TextBox4.Text +"%'";
BindGrid();
} }
}
这样的话在页面载入的时候会根据查询内容装载在DataGrid里并做分页处理,可是我现在的问题是,我有个按钮 这个按钮会做个查询处理 也就是说这个DataGrid可能会装载我查询得到的结果集,可是在按下一页的时候又变成页面装载时的数据了!高手教教我怎么搞吧!

解决方案 »

  1.   

    原因是comstr不能持久化,postabck后恢复到了初始值~ 而Page_Load是每次回传后都会执行的~
      

  2.   

    改两处地方:
    1.
    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
                          if(!IsPostBack)
                          {
    btnFirst.Text = "最首页";
    btnPrev.Text = "前一页";
    btnNext.Text = "下一页";
    btnLast.Text = "最后页";
    ViewState["comstr"]="select *from barcode";
    OpenDatabase();
    BindGrid();
                           }
    }
      

  3.   

    2.
    所有对comstr赋值的地方改为对ViewState["comstr"]赋值;
    所有调用comstr的地方,改为调用ViewState["comstr"].ToString();
    (如SqlCommand cmd=new SqlCommand(ViewState["comstr"].ToString(),con); )