开发学院WEB开发ASP.NET linq 查询分页实例 阅读

linq 查询分页实例

 2010-11-14 08:09:24 来源:WEB开发网   
核心提示:在项目中需要查询分页,今天发布一个经过测试的实例添加一个linq to sql 类DataClasses.dbml新建一个测试页面PageBreakTest.aspx,linq 查询分页实例,具体代码如下:<%@ Page Language="C#" AutoEventWireup="

在项目中需要查询分页,今天发布一个经过测试的实例

添加一个linq to sql 类DataClasses.dbml

新建一个测试页面PageBreakTest.aspx,具体代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageBreakTest.aspx.cs" Inherits="PageBreakTest" %>
<%@ Import Namespace="System.Xml.Linq" %>
<%@ Import Namespace="System.Linq" %>
<% 
    //linq to sql 类
    DataClassesDataContext db = new DataClassesDataContext();
    string ClassID = Request["ClassID"]; //参数 
    ClassID = "100";//我这里没有实际参数,根据自己数据表中的数据随便给了一个值
    int recordcount = 0;
    string StarPage = string.Empty;
    
%>
<html>
<head runat="server">
    <title>Linq分页测试</title>
</head>
<body>
    <ul>
        <%
            try
            {
                //db.tn_article 数据表
                //article_classid 参数对应的字段
                var artcleList = from artcle in db.tn_article where artcle.article_classid == int.Parse(ClassID) orderby artcle.article_id descending select artcle;
                recordcount = artcleList.Count();
                if (recordcount != 0)
                {
                    int Record = string.IsNullOrEmpty(Request["Record"]) ? 20 : int.Parse(Request["Record"]);
                    int PageNum = string.IsNullOrEmpty(Request["PageNum"]) ? 1 : int.Parse(Request["PageNum"]);
                    //分页器 PageBreak(每页显示条数,第几页,总记录数,显示页面地址,参数)
                    StarPage = PageBreak(ref Record, ref PageNum, recordcount, "PageBreakTest.aspx", "<input type=\"hidden\" name=\"ClassID\" value=\"" + Request["ClassID"] + "\"/>");
                    //无参数情况
                    //StarPage = PageBreak(ref Record, ref PageNum, recordcount, "PageBreakTest.aspx", "");
                    //多参数情况
                    //StarPage = PageBreak(ref Record, ref PageNum, recordcount, "PageBreakTest.aspx", "<input type='hidden' name='numberStr' value='" +Request["ClassID"] + "'/><input type='hidden' name='titleStr' value='" + titleStr + "'/><input type='hidden' name='applydept' value='" + applydept + "'/>");            
                    var ta1 = artcleList.Skip(Record * (PageNum - 1)).Take(Record);
                    foreach (tn_article ta in ta1)
                    { 
        %>
        <li>
            <%=ta.article_title%></li>
        <%
            }

             }
             else
             {
                 Response.Write("<li>没有找到相关信息!</li>");
             }
             db.Dispose();
         }
         catch (Exception ex)
         {
             //db.Dispose();
             //Response.Write("<li>请稍候访问!服务器正忙!</li>");  
             throw new Exception(ex.Message);
         }
         
        %>
    </ul>
    <div>
        <%=StarPage%>
    </div>
</body>
</html>

 

后台代码如下(有两个分页方法,一个复杂带跳转,一个简单),当然这两个方法可以放在一个类中任何页面都可以调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class PageBreakTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    //分页器 PageBreak(每页显示条数,第几页,总记录数,显示页面地址,参数)
    public static string PageBreak(ref int Record, ref  int PageNum, int RecordMax, string ActionURL, string Hidden)
    {
        int PageTemp, i, MaxPage;

        System.Text.StringBuilder tmpPageBreak = new System.Text.StringBuilder();

        if (Record > RecordMax)
        {
            Record = RecordMax;
        }
        if (Record < 1)
        {
            Record = 1;
        }

        MaxPage = RecordMax / Record;
        if (RecordMax % Record > 0)
        {
            MaxPage = MaxPage + 1;
        }

        if (PageNum > MaxPage) PageNum = MaxPage;
        if (PageNum < 1) PageNum = 1;

        tmpPageBreak.AppendFormat("<form method=\"get\" action=\"{0}\">", ActionURL);
        tmpPageBreak.Append(Hidden);

        tmpPageBreak.Append("<div style=\"float: left;width:26%;padding: 10px 0px 0px 0px;text-align : center;\">");

        PageTemp = 1;
        if (MaxPage > 1)
        {
            if (PageNum > 1)
            {
                tmpPageBreak.Append("<a href=\"#\" onclick=\"JScript:this.parentNode.parentNode.PageNum.value='1';this.parentNode.parentNode.submit();return false;\">首页</a> ");
                tmpPageBreak.AppendFormat("<a href=\"#\" onclick=\"JScript:this.parentNode.parentNode.PageNum.value='{0}';this.parentNode.parentNode.submit();return false;\">前页</a>", PageNum - 1);
            }
            else
            {
                tmpPageBreak.Append("首页 前页");
            }
            if (PageNum < MaxPage)
            {
                tmpPageBreak.AppendFormat(" <a href=\"#\" onclick=\"JScript:this.parentNode.parentNode.PageNum.value='{0}';this.parentNode.parentNode.submit();return false;\">后页</a> ", PageNum + 1);
                tmpPageBreak.AppendFormat("<a href=\"#\" onclick=\"JScript:this.parentNode.parentNode.PageNum.value='{0}';this.parentNode.parentNode.submit();return false;\">尾页</a>", MaxPage);
                PageTemp = PageNum + 1;
            }
            else
            {
                tmpPageBreak.AppendFormat(" 后页 尾页");
            }
        }
        else
        {
            tmpPageBreak.AppendFormat("首页 前页 后页 尾页");
        }

        tmpPageBreak.AppendFormat("</div>");

        tmpPageBreak.AppendFormat("<div style=\"float: right;width:30%;text-align : center;\">转到:<select name=\"PageNum\">");
        for (i = 1; i <= MaxPage; i++)
        {
            tmpPageBreak.AppendFormat("<option value=\"{0}\"", i);
            if (i == PageTemp)
            {
                tmpPageBreak.Append(" selected=\"selected\"");
            }
            tmpPageBreak.AppendFormat(">{0}</option>", i);
        }

        tmpPageBreak.AppendFormat("</select> <button onclick=\"JScript:this.form.submit();\"><img border=\"0\" src=\"{0}/Images/Goto.Gif\" align=\"absbottom\" /> 翻页</button>", "");
        tmpPageBreak.Append("</div>");

        tmpPageBreak.Append("<div style=\"padding: 3px 0px 0px 0px;text-align : center;\">");
        tmpPageBreak.AppendFormat("共{0}条信息 每页<input maxlength=\"4\" class=\"MyText\" valueType=\"integer\" type=\"text\" name=\"Record\" size=\"2\" value=\"{1}\" />条 页次:<span style=\"font-weight:bold;color:red\">{2}</span>/{3}页", RecordMax, Record, PageNum, MaxPage);

        tmpPageBreak.Append("</div></form>");
        return tmpPageBreak.ToString();
    }
    public static string PageBreak2(ref int Record, ref  int PageNum, int RecordMax, string ActionURL, string Hidden)
    {
        int PageTemp, i, MaxPage;

        System.Text.StringBuilder tmpPageBreak = new System.Text.StringBuilder();

        if (Record > RecordMax)
        {
            Record = RecordMax;
        }
        if (Record < 1)
        {
            Record = 1;
        }

        MaxPage = RecordMax / Record;
        if (RecordMax % Record > 0)
        {
            MaxPage = MaxPage + 1;
        }

        if (PageNum > MaxPage) PageNum = MaxPage;
        if (PageNum < 1) PageNum = 1;

        tmpPageBreak.AppendFormat("<form method=\"get\" action=\"{0}\">", ActionURL);
        tmpPageBreak.Append(Hidden);

        tmpPageBreak.Append("<div style=\"float: left;width:26%;padding: 10px 0px 0px 0px;text-align : center;\">");

        PageTemp = 1;
        if (MaxPage > 1)
        {
            if (PageNum > 1)
            {
                tmpPageBreak.Append("<a href=\"#\" onclick=\"JScript:this.parentNode.parentNode.PageNum.value='1';this.parentNode.parentNode.submit();return false;\">首页</a> ");
                tmpPageBreak.AppendFormat("<a href=\"#\" onclick=\"JScript:this.parentNode.parentNode.PageNum.value='{0}';this.parentNode.parentNode.submit();return false;\">前页</a>", PageNum - 1);
            }
            else
            {
                tmpPageBreak.Append("首页 前页");
            }
            if (PageNum < MaxPage)
            {
                tmpPageBreak.AppendFormat(" <a href=\"#\" onclick=\"JScript:this.parentNode.parentNode.PageNum.value='{0}';this.parentNode.parentNode.submit();return false;\">后页</a> ", PageNum + 1);
                tmpPageBreak.AppendFormat("<a href=\"#\" onclick=\"JScript:this.parentNode.parentNode.PageNum.value='{0}';this.parentNode.parentNode.submit();return false;\">尾页</a>", MaxPage);
                PageTemp = PageNum + 1;
            }
            else
            {
                tmpPageBreak.AppendFormat(" 后页 尾页");
            }
        }
        else
        {
            tmpPageBreak.AppendFormat("首页 前页 后页 尾页");
        }

        tmpPageBreak.AppendFormat("</div>");

        tmpPageBreak.AppendFormat("<div style=\"float: right;width:30%;text-align: center;display:none;\">转到:<select name=\"PageNum\">");
        for (i = 1; i <= MaxPage; i++)
        {
            tmpPageBreak.AppendFormat("<option value=\"{0}\"", i);
            if (i == PageTemp)
            {
                tmpPageBreak.Append(" selected=\"selected\"");
            }
            tmpPageBreak.AppendFormat(">{0}</option>", i);
        }

        tmpPageBreak.AppendFormat("</select> <button onclick=\"JScript:this.form.submit();\"><img border=\"0\" src=\"{0}/Images/Goto.Gif\" align=\"absbottom\" /> 翻页</button>", "");
        tmpPageBreak.Append("</div>");

        tmpPageBreak.Append("<div style=\"padding: 3px 0px 0px 0px;text-align : center;\">");
        tmpPageBreak.AppendFormat("共{0}条信息 每页{1}条 页次:<span style=\"font-weight:bold;color:red\">{2}</span>/{3}页", RecordMax, Record, PageNum, MaxPage);

        tmpPageBreak.Append("</div></form>");
        return tmpPageBreak.ToString();
    }
}


 

.NET2.0下使用LinqMySQL

Tags:linq 查询分页实例

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
更多精彩
    赞助商链接

    热点阅读
      焦点图片
        最新推荐
          精彩阅读