(转)AspNetPager查询分页问题(点击页码,不再是查询后的数据集)viewstate解决

public string SQL = "select * from Memorandum";
      protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              ViewState["SQL"] = SQL;
              BindData();
          }
          if (Request.QueryString["MId"] != null)
          {
              Delete(int.Parse(Request.QueryString["MId"].ToString()));
          }
      }
 
      protected void AspNetPager1_PageChanged(object sender, EventArgs e)
      {
          BindData();
      }
      
      void BindData()
      {
          PagedDataSource pds = new PagedDataSource();
          IList<MemorandumInfo> Infos = new DAL.Memorandum().GetAllMemorandumsBySQL(ViewState["SQL"].ToString());
 
          pds.DataSource = Infos;
          pds.AllowPaging = true;
          pds.PageSize = 10;//取控件的分页大小
          pds.CurrentPageIndex = this.AspNetPager1.CurrentPageIndex - 1;//显示当前页
          //设置控件
          this.AspNetPager1.RecordCount = Infos.Count;//记录总数
          this.AspNetPager1.PageSize = 10;
          Repeater1.DataSource = pds;
          Repeater1.DataBind();
      }
      void Delete(int MId)
      {
          new Memorandum().DeleteMemorandum(MId);
          Response.Write("<script>alert('成功删除该条备忘信息');location='?';</script>");
      }
      /// <summary>
      /// 查询
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
 
      protected void Button1_Click(object sender, EventArgs e)
      {
          if (DropDownList1.SelectedValue == "0")
          {
              ViewState["SQL"] = "select * from Memorandum";
              BindData();
          }
          else
          {
              ViewState["SQL"] = "SELECT   MId, MemoTitle, MemoContent, MemoStartTime, MemoEndTime FROM  Memorandum WHERE (MemoEndTime - NOW() <=7)";
              BindData();
          }
      }
  }

ViewState 用来跟踪和保存控件的状态信息。否则这些信息可能会丢失,原因可能是这些值不随着 form 回发,或者根本就不在 page 的 html 中。
ViewState 中保存着代码中改变的控件属性,通过代码绑定到控件的任何数据,以及由用户操作触发,回发的任何更改。
ViewState 还提供了一个状态包(StateBag), 这是一个特殊的集合或字典(collection or dictionary), 可以用来保存,通过一个 key 来恢复任意的对象或者值。

赋值:ViewState[key] = value;

取值:value = ViewState[key];

原文地址:https://www.cnblogs.com/wanshutao/p/3941286.html