[转帖]ASP.NET大数据量查询分页例子_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3081 | 回复: 0   主题: [转帖]ASP.NET大数据量查询分页例子        下一篇 
张三丰
注册用户
等级:上尉
经验:511
发帖:53
精华:0
注册:2013-11-19
状态:离线
发送短消息息给张三丰 加好友    发送短消息息给张三丰 发消息
发表于: IP:您无权察看 2013-11-21 9:45:11 | [全部帖] [楼主帖] 楼主

分页这个也是最多人问的,也是很基础很实用的。
网上有很多分页代码,要不是有前台就没后台,要不是有后台没前台,要不是就是控件,要不就是一大堆SQL代码,让人不知道怎样用。
力求最简单最易懂.三层架构那些就不搞了。

前台 如果你用的是 gridview 就把Repeater替换成gridview 。一样的。。

<formid="frm01"action=""method="post"runat="server">
<asp:ScriptManagerID="ScriptManager1"runat="server">
</asp:ScriptManager>
<asp:UpdatePanelID="UpdatePanel1"runat="server">
<ContentTemplate>
<divid="center-top"> <divclass="fon">中间1</div> </div>
<divid="center-content_Default2">
<asp:Repeaterrunat="server"Id="mainlist">
<ItemTemplate>
<divid="pic-out">
<liclass="pic-a">  Eval("product_type").ToString() </li>
<liclass="pic-b">Eval("product_desc").ToString() </li>
<li>价格<%#Eval("price")%> </li>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
<divstyle="PADDING-TOP:10px; text-align:center">


&nbsp; 共<asp:labelid="LPageCount"ForeColor="#ff0000"Runat="server"></asp:label>页

<asp:labelid="LTotalCount"ForeColor="#ff0000"Runat="server"></asp:label>条记录


<asp:linkbuttonid="Fistpage"Runat="server"CommandName="0"OnClick="Pager_Click">首頁</asp:linkbutton>

<asp:linkbuttonid="Prevpage"Runat="server"CommandName="prev"OnClick="Pager_Click">上一頁</asp:linkbutton>

<asp:linkbuttonid="Nextpage"Runat="server"CommandName="next"OnClick="Pager_Click">下一頁</asp:linkbutton>

<asp:linkbuttonid="Lastpage"Runat="server"CommandName="last"OnClick="Pager_Click">尾頁</asp:linkbutton>当前第

<asp:labelid="LCurrentPage"ForeColor="#ff0000"Runat="server"></asp:label>頁


&nbsp; 转到第

<asp:textboxid="gotoPage"Width="30px"Runat="server"AutoPostBack="True"MaxLength="5"></asp:textbox>頁

<asp:Labelstyle=" POSITION: absolute"id="msgbox"runat="server"ForeColor="Red"BorderColor="Red"></asp:Label>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</form>
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.Data.OleDb;
usingSystem.Text;
usingMicrosoft.ApplicationBlocks.Data;
publicpartial class_Default : System.Web.UI.Page
{
      stringstrpage = "";
      intPageCount = 0;
      intRecCount = 0;
      intCurrentPage = 0;
      intCurrent = 0;
      intPages = 0;
      intJumpPage = 0;//跳到第几页
      //每页显示记录数
      intPageSize =20; //每页显示20条记录
      stringproduct_type = "";
      stringtype = "";
      stringheadid = "";
      privatestaticintPageIndex=1;
      privatestaticintPageCounts=1;
      privatestaticintJumpPages=1;
      protectedvoidPage_Load(objectsender, EventArgs e)
      {
            #region 接收从查询页面传递过来的参数。
            if(Request.QueryString["product_type"]!=null)
            {
                  product_type = Server.UrlDecode(Request.QueryString["product_type"].ToString());
            }
            if(Request.QueryString["type"]!=null)
            {
                  type = Server.UrlDecode(Request.QueryString["type"].ToString());
            }
            #endregion
            // headid = Request.QueryString["headid"];
            if(!IsPostBack)
            {
                  //-------------------------------------------------------------
                  RecCount = Calc();
                  //计算总记录数
                  PageCount = RecCount / PageSize + OverPage();
                  //计算总页数
                  PageCounts = RecCount / PageSize - ModPage();
                  if(!string.IsNullOrEmpty(strpage))
                  {
                        //设置当前页为返回页
                        PageIndex= int.Parse(strpage);
                  }
                  else
                  {
                        PageIndex= 1;
                        //设置当前页为11
                        //Session["CurPage"] = 1;
                  }
                  JumpPages = PageCount;
                  LPageCount.Text = PageCount.ToString();
                  //总页数
                  LTotalCount.Text = RecCount.ToString();
                  //总记录数
                  if(RecCount <= PageSize)
                  {
                        gotoPage.Enabled = false;
                  }
                  else
                  {
                        gotoPage.Enabled = true;
                  }
                  //-------------------------------------------------------------
                  GetMainData(); //绑定数据
            }
      }
      #region "計算總行數"
      publicintOverPage()
      {
            //算余
            intpages = 0;
            if(RecCount % PageSize != 0) {
                  pages = 1;
            }
            else{
                  pages = 0;
            }
            returnpages;
      }
      publicintModPage()
      {
            //算余
            intpages = 0;
            if(RecCount % PageSize == 0 && RecCount != 0)
            {
                  pages = 1;
            }
            else{
                  pages = 0;
            }
            returnpages;
      }
      publicintCalc()
      {
            //計算記錄總數
            DataSet ds= newDataSet();
            StringBuilder ass = newStringBuilder("Select count(id) from M_product where 1=1 ");
            //如果有查询参数传入,就计算查询结果的总数
            if(type != null&& type != "")
            {
                  ass.Append(" and [type]="+ type.Replace("'", "") + " ");
            }
            if(product_type != null&& product_type != "")
            {
                  ass.Append(" and product_type="+ product_type.Replace("'", "") + " ");
            }
            stringbss = ass.ToString();
            intRecordCount = 0;
            ds = SqlHelper.ExecuteDataset(SqlHelper.Conn, CommandType.Text, bss);
            RecordCount = Int32.Parse(ds.Tables[0].Rows[0][0].ToString());
            //RecordCount = 10;
            if(RecordCount < PageSize)
            {
                  Fistpage.Enabled = false;
                  Prevpage.Enabled = false;
                  Nextpage.Enabled = false;
                  Lastpage.Enabled = false;
            }
            else
            {
                  Fistpage.Enabled = true;
                  Prevpage.Enabled = true;
                  Nextpage.Enabled = true;
                  Lastpage.Enabled = true;
            }
            returnRecordCount;
      }
      #endregion
      #region "翻頁"
      publicvoidPager_Click(objectsender, EventArgs e)
      {
            CurrentPage = (int)PageIndex;
            Pages = (int)PageCounts;
            stringarg = ((LinkButton)sender).CommandName.ToString();
            switch(arg) {
                  case"next":
                  //下一页   如果要支持URL分页。 只要把CurrentPage 从URL获取就可以了
                  if(CurrentPage < Pages + 1)
            { CurrentPage = CurrentPage + 1; }
                  break;
                  case"prev":
                  //上一页
                  if(CurrentPage != 1)
            { CurrentPage -= 1; }
                  break;
                  case"last":
                  //最后一页
                  CurrentPage = Pages + 1;
                  break;
                  default:
                  //首页
                  CurrentPage = 1;
                  break;
            }
            //��据页数控制翻页按钮的可用与否
            if(CurrentPage > 1) {
                  Fistpage.Enabled = true;
                  Prevpage.Enabled = true;
            }
            else{
                  Fistpage.Enabled = false;
                  Prevpage.Enabled = false;
            }
            if(CurrentPage == Pages + 1)
            {
                  Nextpage.Enabled = false;
                  Lastpage.Enabled = false;
            }
            else{
                  Nextpage.Enabled = true;
                  Lastpage.Enabled = true;
            }
            PageIndex= CurrentPage;
            //获取改變后的页码
            // //Session["CurPage"] = CurrentPage;
            //用户返回到当前页
            GetMainData();
      }
      //转到第几页
      protectedvoidgotoPage_TextChanged(objectsender, System.EventArgs e)
      {
            stringasd = this.gotoPage.Text.Trim().ToString();
            JumpPage = (int)JumpPages;
            if(string.IsNullOrEmpty(asd)) {
                  Alert("out of page range");
                  return;
            }
            if(Int32.Parse(gotoPage.Text) > JumpPage || Int32.Parse(gotoPage.Text) <= 0 || string.IsNullOrEmpty(asd))
            {
                  Alert("out of page range");//页数超出范围
                  return;
            }
            else{
                  intInputPage = Int32.Parse(gotoPage.Text.ToString());
                  PageIndex= InputPage;
                  ////Session["CurPage"] = InputPage;
                  GetMainData();//绑定数据集
            }
      }
      #endregion
      publicvoidAlert(stringrtt)
      {
            //顯示提示信息
            // System.Web.UI.ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "AjaxMsgBox", "alert('" + rtt + "');", true);
      }
      privatevoidGetMainData()
      {
            CurrentPage = (int)PageIndex;//获取当前页
            Pages = (int)PageCounts;//获取总页数
            LCurrentPage.Text = CurrentPage.ToString();
            intPageSize2 = PageSize * (CurrentPage - 1)+1;
            if(type != null&& product_type!=null)//如果是手机
            {
                  //用于函数参数的个数不对 在查询表达式 'isnull(max(xx.id),0)' 中。
                  // SELECT 子句中包含一个保留字、拼写错误或丢失的参数,或标点符号不正确。
                  //分页的核心语句,决定性能   row_number()也不错。也可以将以下语句改为存储过程。
                  StringBuilder sql = newStringBuilder("select top "+ PageSize + " * from M_product a ");
                  sql.Append(" where a.[id]>( select max(xx.[id]) from ( select TOP "+ PageSize2 + " [id] from M_product where 1=1 ");
                  if(type != null&& type != "")
                  {
                        sql.Append(" and [type]="+ type.Replace("'", "")+"");
                  }
                  if(product_type != null&& product_type != "")
                  {
                        sql.Append(" and product_type="+ product_type.Replace("'", "") + "");
                  }
                  sql.Append(" ORDER BY [id] )xx ) ");
                  if(type != null&& type != "")
                  {
                        sql.Append(" and a.[type]="+ type.Replace("'", "") + "");
                  }
                  if(product_type != null&& product_type != "")
                  {
                        sql.Append(" and a.product_type="+ product_type.Replace("'", "") + "");
                  }
                  sql.Append(" ORDER BY a.[id] ");
                  mainlist.DataSource = SqlHelper.ExecuteDataset( SqlHelper.Conn, CommandType.Text,sql.ToString());
                  mainlist.DataBind();
            }
      }
}




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论