zoukankan      html  css  js  c++  java
  • (转)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];

  • 相关阅读:
    Windows10系统下在Docker中部署静态网站
    NET接入Mesher--------解决微服务什么时候能支持.net 的应用开发的问题
    Windows系统下 ASP.NET Core 的 Docker 映像创建
    Windows平台部署 Asp.Net Core 3.1.0,将 ASP.NET Core 应用发布到 IIS ,使用 IIS 在 Windows 上托管 ASP.NET Core
    如何使用Activator.CreateInstance创建一个列表<T>,其中T在运行时是未知的?
    在 Visual Studio 中安装 FxCop 分析器
    .Net Core3.1下使用Swagger搭建web api项目
    Vuex访问状态对象的方法
    Vuex最基本样例
    搜索框 展示相关搜索内容列表,及延迟改进(仿百度)
  • 原文地址:https://www.cnblogs.com/wanshutao/p/3941286.html
Copyright © 2011-2022 走看看