NVelocity分页

来源:互联网 发布:通达oa精灵mac版 编辑:程序博客网 时间:2024/05/16 08:43

下载Nvelocity http://sourceforge.net/projects/castleproject/files/NVelocity/1.1/CastleNVelocity-1.1.0.zip/download

NVelocity封装类VelocityHelper:

using System;using System.Web;using System.IO;using NVelocity;using NVelocity.App;using NVelocity.Context;using NVelocity.Runtime;using Commons.Collections;    /// <summary>    /// NVelocity模板工具类 VelocityHelper    /// </summary>    public class VelocityHelper    {        private VelocityEngine velocity = null;        private IContext context = null;        /// <summary>        /// 构造函数        /// </summary>        /// <param name="templatDir">模板文件夹路径</param>        public VelocityHelper(string templatDir)        {            Init(templatDir);        }        /// <summary>        /// 无参数构造函数        /// </summary>        public VelocityHelper() { ;}        /// <summary>        /// 初始话NVelocity模块        /// </summary>        /// <param name="templatDir">模板文件夹路径</param>        public void Init(string templatDir)        {            //创建VelocityEngine实例对象            velocity = new VelocityEngine();            //使用设置初始化VelocityEngine            ExtendedProperties props = new ExtendedProperties();            props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");            props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, HttpContext.Current.Server.MapPath(templatDir));            props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");            props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");            velocity.Init(props);            //为模板变量赋值            context = new VelocityContext();        }        /// <summary>        /// 给模板变量赋值        /// </summary>        /// <param name="key">模板变量</param>        /// <param name="value">模板变量值</param>        public void Put(string key, object value)        {            if (context == null)                context = new VelocityContext();            context.Put(key, value);        }        /// <summary>        /// 显示模板        /// </summary>        /// <param name="templatFileName">模板文件名</param>        public void Display(string templatFileName)        {            Template template;            //try            //{                //从文件中读取模板                template = velocity.GetTemplate(templatFileName);            //}            //catch            //{            //    template = velocity.GetTemplate(templatFileName.Split('.')[0].ToString()+".htm");            //}            //合并模板            StringWriter writer = new StringWriter();            template.Merge(context, writer);            //输出            HttpContext.Current.Response.Clear();            HttpContext.Current.Response.Write(writer.ToString());            HttpContext.Current.Response.Flush();            HttpContext.Current.Response.End();        }    }


我的分页代码PageSplit.cs

using System;using System.Collections.Generic;using Maticsoft.DBUtility;using System.Data;using System.Data.SqlClient;using NVelocity;using NVelocity.App;using NVelocity.Context;using NVelocity.Runtime;using Commons.Collections;namespace Gwbn.Common{    /// <summary>    /// 分页    /// </summary>    public class PageSplit    {        /// <summary>        /// 记录条数        /// </summary>        /// <param name="query">条件</param>        /// <param name="table">表名</param>        /// <returns>记录数</returns>        public int GetCount(string query, string table)        {            SqlParameter[] parames = new SqlParameter[0];            string sql = "select count(*) from " + table;            if (query.Trim().Length > 0)            {                sql = sql + " where " + query;            }            DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.SqlConncectionString, CommandType.Text, sql, parames);            int count = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());            return count==0?1:count;          }        /// <summary>        /// 计算总页数        /// </summary>        /// <param name="count">总记录数</param>        /// <param name="pagesize">每页记录数</param>        /// <returns>总页数</returns>        public int GetPages(int count, int pagesize)        {            if (count % pagesize == 0)                return count / pagesize;            else                return count / pagesize + 1;        }        /// <summary>        /// 获取上一页页码        /// </summary>        /// <param name="current">当前页</param>        /// <returns></returns>        public int GetPagePrev(int current,int pages)        {            if (current > 1 && current < pages)                return current - 1;            else if (current > pages)                return pages - 1;            else                return 1;        }        /// <summary>        /// 获取下一页页码        /// </summary>        /// <param name="current">当前页</param>        /// <param name="pages">总页数</param>        /// <returns></returns>        public int GetPageNext(int current,int pages)        {            if (current < pages)                return current + 1;            else                return pages;        }        /// <summary>        /// 分页条显示开始页        /// </summary>        /// <param name="current">当前页</param>        /// <param name="pages">总页数</param>        /// <param name="pagenumbers">显示几页</param>        /// <returns></returns>        public int GetPageFirst(int current, int pages, int pagenumbers)        {            int mid = pagenumbers / 2;            if (pages <= pagenumbers)                return 1;            else            {                int first = (current - mid) > 1 ? (current - mid) : 1;                return first>(pages-pagenumbers + 1)?(pages-pagenumbers + 1):first ;            }        }        /// <summary>        /// 分页条显示结束页        /// </summary>        /// <param name="current">当前页</param>        /// <param name="pages">总页数</param>        /// <param name="pagenumbers">显示几页</param>        /// <returns></returns>        public int GetPageLast(int current, int pages, int pagenumbers)        {            int mid = pagenumbers / 2;            if (pages <= pagenumbers)                return pages;            else            {                int last = ((current+mid) < pagenumbers) ? pagenumbers : (current + mid);                return last<pages?last:pages;            }        }        /// <summary>        /// 设置分页参数        /// </summary>        /// <param name="vh"></param>        /// <param name="table">表名称</param>        /// <param name="query">查询条件</param>        /// <param name="current">当前页码</param>        public void SetPage(VelocityHelper vh, string table, string query ,int current)        {            vh.Put("page", current);            vh.Put("count", GetCount(query, table));//获取记录数            vh.Put("pager", new PageSplit());        }    }}


我的分页模板页面_pager.htm

        <div id="pager">            #set($pages = $pager.GetPages($count,$pagesize))             #set($pagenumbers = 5)  ##显示多少页码            #set($pagestart =  $pager.GetPageFirst($page,$pages,$pagenumbers))             #set($pageend = $pager.GetPageLast($page,$pages,$pagenumbers))                <div class="pager">                    <a href="?type=$module&class=$infoClass&page=1" mce_href="?type=$module&class=$infoClass&page=1" title="第一页" class="btn pageFitst"></a>                    <a href="?type=$module&class=$infoClass&page=$pager.GetPagePrev($page,$pages)" mce_href="?type=$module&class=$infoClass&page=$pager.GetPagePrev($page,$pages)" title="上一页" class="btn pagePrev"></a>                    <span>                    #foreach($i in [$pagestart..$pageend])                        #if ($i == $pagestart)                           #if ($i == $page)                                <a href="?type=$module&class=$infoClass&page=$i" mce_href="?type=$module&class=$infoClass&page=$i"  class="nl current">$i</a>                           #else                                <a href="?type=$module&class=$infoClass&page=$i" mce_href="?type=$module&class=$infoClass&page=$i"  class="nl">$i</a>                           #end                        #else                            #if ($i == $page)                                <a href="?type=$module&class=$infoClass&page=$i" mce_href="?type=$module&class=$infoClass&page=$i" ) class="current">$i</a>                            #else                                <a href="?type=$module&class=$infoClass&page=$i" mce_href="?type=$module&class=$infoClass&page=$i" )>$i</a>                             #end                        #end                    #end                    </span>                    <a href="?type=$module&class=$infoClass&page=$pager.GetPageNext($page,$pages)" mce_href="?type=$module&class=$infoClass&page=$pager.GetPageNext($page,$pages)" title="下一页" class="btn pageNext"></a>                    <a href="?type=$module&class=$infoClass&page=$pages" mce_href="?type=$module&class=$infoClass&page=$pages" title="最后一页" class="btn pageLast"></a>                    <select  onchange="MM_jumpMenu(this,'$module','$infoClass')">                    #foreach($i in [1..$pages])                        #if ($i == $page)                    <option selected="selected">第$i页</option>                    #else                        <option value="$i">第$i页</option>                        #end                    #end                    </select>                </div>            </div>            <mce:script type="text/javascript"><!--                function MM_jumpMenu(selObj, module,infoclass) {                    top.location="?type="+module+"&class="+infoclass+"&page=" + selObj.options[selObj.selectedIndex].value;                }            // --></mce:script>


示例infoDAL

using System;using System.Collections.Generic;using System.Data.SqlClient;using Maticsoft.DBUtility;using System.Data;/// <summary>///info 的摘要说明/// </summary>public class InfoDAL{public InfoDAL(){////TODO: 在此处添加构造函数逻辑//}    /// <summary>    /// start从第几条记录开始取值,limit一次取多少    /// </summary>    /// <param name="query">查询条件</param>    /// <param name="limit">记录数</param>    /// <param name="current">当前页或开始条</param>    /// <param name="isPage">是不是按页取 true是 flase否(按条数)</param>    /// <returns></returns>    public List<Info> GetList(string query, int limit, int current, bool isPage)    {        List<Info> _info = new List<Info>();        string json = string.Empty;        int start = 0;        if (current > 1 && isPage)            start = (current - 1) * limit;        else            start = current - 1;        SqlParameter[] parames = new SqlParameter[0];        string selSql = "select top " + limit.ToString() + " title,id from info where id not in(select top " + start.ToString() + " id from info   order by id )  order by id";        if (query.Trim().Length > 0)        {            selSql = "select top " + limit.ToString() + " title,id from info where id not in(select top " + start.ToString() + " id from info  where " + query + " order by id) and  (" + query + ")  order by  id";        }        DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.SqlConncectionString, CommandType.Text, selSql, parames);        if (ds != null && ds.Tables[0].Rows.Count > 0)        {            foreach (DataRow row in ds.Tables[0].Rows)            {                Info info = new Info();                info.id = Convert.ToInt32(row["id"]);                info.title = Convert.ToString(row["title"]);                _info.Add(info);            }        }        return _info;    }}[Serializable]public class Info{    private int _id;    private string _title;    /// <summary>    ///     /// </summary>    public int id    {        set { _id = value; }        get { return _id; }    }    /// <summary>    ///     /// </summary>    public string title    {        set { _title = value; }        get { return _title; }    }}


使用示例

index.htm

#parse("_head.htm")<!--begin main--><div id="main"><div class="list">    <ul>     #set( $pagesize = 10 )      #foreach($info in $infos.GetList("", $pagesize, $page,true))        <li>$info.title</li>        #set($value = $velocityCount)     #end    </ul></div>  <div id="page">     #if ( $page>1 || $value>=10)        #parse("_pager.htm")     #end</div>  </div><!--end main-->#parse("_foot.htm")

index.cs

using System;using Sys.Common;public partial class _Default : System.Web.UI.Page {    protected void Page_Load(object sender, EventArgs e)    {        int page = 1;        try        {            page = Convert.ToInt32(Request.QueryString["page"].ToString());        }        catch        { }        VelocityHelper vh = new VelocityHelper();        vh.Init(@"~/Templates");        PageSplit pager = new PageSplit();        pager.SetPage(vh, "Info", "", page);//分页计算        vh.Put("infos", new InfoDAL());        vh.Display("index.htm");    }}


该实例下载地址:http://download.csdn.net/detail/yanzhibo/6925401

原文:http://blog.csdn.net/windxxf/article/details/5969811

0 0
原创粉丝点击