打印本文 打印本文 关闭窗口 关闭窗口
asp.net(C#)海量数据表高效率分页算法(易懂,不使用存储过程)
作者:武汉SEO闵涛  文章来源:敏韬网  点击数1819  更新时间:2009/4/23 10:39:14  文章录入:mintao  责任编辑:mintao
算后的CurrentPage变量再次保存至ViewState
   TDataBind();//调用数据绑定函数TDataBind()
  }

  private void TDataBind()
  {
   CurrentPage = (int)ViewState["PageIndex"];//从ViewState中读取页码值保存到CurrentPage变量中进行按钮失

效运算
   Pages = (int)ViewState["PageCounts"];//从ViewState中读取总页参数进行按钮失效运算
   //判断四个按钮(首页、上一页、下一页、尾页)状态
   if (CurrentPage + 1 > 1)
   {
    Fistpage.Enabled = true;
    Prevpage.Enabled = true;
   }
   else
   {
    Fistpage.Enabled = false;
    Prevpage.Enabled = false;
   }
   if (CurrentPage == Pages)
   {
    Nextpage.Enabled = false;
    Lastpage.Enabled = false;
   }
   else
   {
    Nextpage.Enabled = true;
    Lastpage.Enabled = true;
   }
            //数据绑定到DataList控件
   DataSet ds = new DataSet();
   //核心SQL语句,进行查询运算(决定了分页的效率:))
   SqlDataAdapter MyAdapter = new SqlDataAdapter("Select Top "+PageSize+" * from redheadedfile where id

not in(select top "+PageSize*CurrentPage+" id from redheadedfile order by id asc) order by id asc",MyCon());
   MyAdapter.Fill(ds,"news");
   datalist1.DataSource = ds.Tables["news"].DefaultView;
   datalist1.DataBind();
   //显示Label控件LCurrentPaget和文本框控件gotoPage状态
   LCurrentPage.Text = (CurrentPage+1).ToString();
   gotoPage.Text = (CurrentPage+1).ToString();
   //释放SqlDataAdapter
   MyAdapter.Dispose();
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Fistpage.Command += new System.Web.UI.WebControls.CommandEventHandler(this.Page_OnClick);
   this.Prevpage.Command += new System.Web.UI.WebControls.CommandEventHandler(this.Page_OnClick);
   this.Nextpage.Command += new System.Web.UI.WebControls.CommandEventHandler(this.Page_OnClick);
   this.Lastpage.Command += new System.Web.UI.WebControls.CommandEventHandler(this.Page_OnClick);
   this.gotoPage.TextChanged += new System.EventHandler(this.gotoPage_TextChanged);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
        //跳页代码
  private void gotoPage_TextChanged(object sender, System.EventArgs e)
  {
   try
   {
    JumpPage = (int)ViewState["JumpPages"];//从ViewState中读取可用页数值保存到JumpPage变量中
    //判断用户输入值是否超过可用页数范围值
    if(Int32.Parse(gotoPage.Text) > JumpPage || Int32.Parse(gotoPage.Text) <= 0)
     

Response.Write("<script>alert(''''页码范围越界!'''');location.href=''''WebForm8.aspx''''</script>");
    else
    {
     int InputPage = Int32.Parse(gotoPage.Text.ToString()) - 1;//转换用户输入值保存在int型

InputPage变量中
     ViewState["PageIndex"] = InputPage;//写入InputPage值到ViewState["PageIndex"]中
     TDataBind();//调用数据绑定函数TDataBind()再次进行数据绑定运算
    }
   }
      //捕获由用户输入不正确数据类型时造成的异常
   catch(Exception exp)
   {
    Response.Write("<script>alert(''''"+exp.Message+"'''');location.href=''''WebForm8.aspx''''</script>");
   }
  }
 }
}

大家来试试,效率是不是高了很多?

如有不妥望大家来指正

上一页  [1] [2] 

打印本文 打印本文 关闭窗口 关闭窗口