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];

  • 相关阅读:
    最新屏蔽微信举报方法
    C# WIN 生成机器码
    Quartz.net的快速简单上手使用以及防止IIS回收停止Job的处理
    MVC、Web API 请求接口报错“自定义错误模块不能识别此错误。”解决
    获取微信短链接的官方接口
    Window 通过cmd查看端口占用、相应进程、杀死进程
    微信域名检测、防封,微信跳转技术揭秘(二) -- 微信跳转揭秘
    微信域名检测、防封,微信跳转技术揭秘(一) -- 域名检测原理及防封方案
    各种比较方便给力的小工具
    《Git学习指南》学习笔记(三)
  • 原文地址:https://www.cnblogs.com/wanshutao/p/3941286.html
Copyright © 2011-2022 走看看