zoukankan      html  css  js  c++  java
  • 自定义GridView分页控件

    此控件使用Sql的ROW_NUMBER函数简单实现GridView的自定义分页功能,不考虑数据库及列表控件的兼容、通用性,如果有这方面的需要,也可以据此方式进行扩展和完善。

    一、控件呈现端代码:
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="PageSplit.ascx.cs" Inherits="ASCX_PageSplit" %>

    <div style="text-align: center;">
        <asp:LinkButton ID="lbtnFirst" runat="server" OnClick="lbtnFirst_Click">第一页</asp:LinkButton>&nbsp;
        <asp:LinkButton ID="lbtnUp" runat="server" OnClick="lbtnUp_Click">上一页</asp:LinkButton>&nbsp;
        <asp:LinkButton ID="lbtnNext" runat="server" OnClick="lbtnNext_Click">下一页</asp:LinkButton>&nbsp;
        <asp:LinkButton ID="lbtnLast" runat="server" OnClick="lbtnLast_Click">最后一页</asp:LinkButton>&nbsp;&nbsp;
        <asp:Label ID="lblPageCount" runat="server"></asp:Label>&nbsp;&nbsp; 第<asp:DropDownList
            ID="ddlGo" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlGo_SelectedIndexChanged">
        </asp:DropDownList>
        页
    </div>

    二、控件事件代码:
    public partial class ASCX_PageSplit : System.Web.UI.UserControl
    {
        /// <summary>
        /// 分页事件
        /// </summary>
        public event PageChangeDelegate PageChange;
        /// <summary>
        /// 分页事件委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public delegate void PageChangeDelegate(object sender, EventArgs e);
        /// <summary>
        /// 获取或设置当前页索引
        /// </summary>
        public int CurrentIndex
        {
            get { return ViewState["CurrentIndex"] == null ? 0 : Convert.ToInt32(ViewState["CurrentIndex"]); }
            set { ViewState["CurrentIndex"] = value; }
        }
        /// <summary>
        /// 获取或设置每页显示记录数
        /// </summary>
        public int PageSize
        {
            get { return ViewState["PageSize"] == null ? 20 : Convert.ToInt32(ViewState["PageSize"]); }
            set { ViewState["PageSize"] = value; }
        }
        /// <summary>
        /// 获取或设置查询语句
        /// </summary>
        public string CommandString
        {
            get { return ViewState["CommandString"] == null ? "" : ViewState["CommandString"].ToString(); }
            set { ViewState["CommandString"] = value; }
        }
        /// <summary>
        /// 获取或设置排序字段
        /// </summary>
        public string OrderName
        {
            get { return ViewState["OrderName"] == null ? "" : ViewState["OrderName"].ToString(); }
            set { ViewState["OrderName"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <returns></returns>
        public string BindData(GridView gvCtrl)
        {
            int pageCount = 0, recordCound = 0;
            DataTable resultData = GetData(ref pageCount, ref recordCound);
            if (resultData == null || resultData.Rows.Count == 0)
            {
                gvCtrl.DataSource = null;
                gvCtrl.DataBind();
                return "没有查询结果!";
            }

            if (ddlGo.Items.Count != pageCount)
            {
                ddlGo.Items.Clear();
                for (int i = 0; i < pageCount; i++)
                    ddlGo.Items.Add((i + 1).ToString());
            }

            ddlGo.SelectedIndex = CurrentIndex;
            lblPageCount.Text = "共 " + pageCount.ToString() + " 页 " + recordCound.ToString() + "条记录";

            if (resultData.Rows.Count == recordCound)
            {
                lbtnFirst.Enabled = false;
                lbtnUp.Enabled = false;
                lbtnNext.Enabled = false;
                lbtnLast.Enabled = false;
            }
            else
            {
                lbtnFirst.Enabled = true;
                lbtnUp.Enabled = true;
                lbtnNext.Enabled = true;
                lbtnLast.Enabled = true;
            }
            lbtnFirst.Enabled = CurrentIndex == 0 ? false : true;
            lbtnUp.Enabled = CurrentIndex == 0 ? false : true;
            lbtnLast.Enabled = CurrentIndex == pageCount - 1 ? false : true;
            lbtnNext.Enabled = CurrentIndex == pageCount - 1 ? false : true;

            gvCtrl.DataSource = resultData;
            gvCtrl.DataBind();

            return string.Empty;
        }

        /// <summary>
        /// 分页跳转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlGo_SelectedIndexChanged(object sender, EventArgs e)
        {
            CurrentIndex = ddlGo.SelectedIndex;
            this.PageChange(this, null);
        }
        /// <summary>
        /// 第一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnFirst_Click(object sender, EventArgs e)
        {
            CurrentIndex = 0;
            this.PageChange(this, null);
        }
        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnUp_Click(object sender, EventArgs e)
        {
            CurrentIndex -= 1;
            this.PageChange(this, null);
        }
        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            CurrentIndex += 1;
            this.PageChange(this,null);
        }
        /// <summary>
        /// 最后一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnLast_Click(object sender, EventArgs e)
        {
            CurrentIndex = ddlGo.Items.Count - 1;
            this.PageChange(this, null);
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="pageCount">页数</param>
        /// <param name="recordCound">数据记录数</param>
        /// <returns></returns>
        private DataTable GetData(ref int pageCount, ref int recordCound)
        {
            if (string.IsNullOrEmpty(CommandString))
                return null;

            DataTable resultData = null;
            SqlConnection conn = null;
            SqlDataReader reader = null;

            string strSqlTmp = "select count(" + OrderName + ") as RECORDCOUNT from (" + CommandString + ") AAA";

            try
            {
                conn = new SqlConnection(ConfigurationSettings.AppSettings["Connection"]);
                SqlCommand comm = conn.CreateCommand();
                comm.CommandText = strSqlTmp;

                conn.Open();
                object obj = comm.ExecuteScalar();
                if (obj == null || string.IsNullOrEmpty(obj.ToString()))
                    return null;

                recordCound = Convert.ToInt32(obj);
                if (recordCound % PageSize != 0)
                    pageCount = recordCound / PageSize + 1;
                else
                    pageCount = recordCound / PageSize;

                int pageUpper = PageSize * (CurrentIndex + 1);
                int pageLower = PageSize * CurrentIndex;

                string sql = "select * from (SELECT ROW_NUMBER() OVER(ORDER BY " + OrderName + " ASC) As rownum,AAA.* FROM (" + CommandString + ") AS AAA) AS BBB WHERE rownum <= " + pageUpper.ToString() + " and rownum>" + pageLower.ToString();
                comm.CommandText = sql;
                resultData=new DataTable();
                SqlDataAdapter adapter = new SqlDataAdapter(comm);
                adapter.Fill(resultData);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (reader != null && !reader.IsClosed)
                    reader.Close();
                if (conn != null && conn.State == ConnectionState.Open)
                    conn.Close();
            }

            return resultData;
        }

    }

    三、使用方式

    1.引用控件:<%@ Register Src="~/ASCX/PageSplit.ascx" TagName="PageSplit" TagPrefix="ps" %>
    2.将控件放到GridView控件之后:
    <asp:GridView>
     ... ...
    </asp:GridView>
    <ps:PageSplit ID="pSplit" runat="server" OnPageChange="pSplit_PageChange"/>

    3.服务器端,比如查询事件中

     ... ...

      string sql = "***";
            pSplit.CommandString = sql;
            pSplit.OrderName = "ContractFormCode";
            pSplit.CurrentIndex = 0;
            string message = pSplit.BindData(gvContractList);
            divContractList.Visible = gvContractList.Rows.Count > 0;

     ... ...

    4.分页响应事件
     protected void pSplit_PageChange(object sender, EventArgs e)
            {
             pSplit.BindData(gvContractList);
            }

    四、效果图

    自定义GridView分页控件 - 子言 - 子言的博客
     
     
     
     
     

    http://blog.163.com/y_p_xu/blog/static/170857102201132952932735/

  • 相关阅读:
    Lucene实践之IndexFile
    java利用pdfbox处理pdf
    java通过dom4j处理xml文档
    CouchBase 2.5Kb数据无法编辑的解决办法
    Strutes2标签url
    Strutes2标签iterator
    2.23Java基础总结 ①内存图,内存加载过程②内存叠加③动态绑定
    2.23 Java基础总结 ①toString方法②equals③this和super④final⑤常量
    数学趣题——魔幻方阵
    数学趣题——谁在说谎
  • 原文地址:https://www.cnblogs.com/YoungPop-Chen/p/3388267.html
Copyright © 2011-2022 走看看