dr_twilight

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

         一.   DAL

///
///分页sql
///  
public DataTable GetUserAndError(int pageSize, int pageIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY t.CONTENT , t.Username ) AS id , t.* FROM  ");
            strSql.Append("(SELECT CONTENT , Username , COUNT(1) AS Num  FROM ");
            strSql.Append("Ticket_Log4net_Database.dbo.tbl_Interface_HandleLog WITH ( NOLOCK ) ");
            strSql.Append("WHERE LOGTIME > DATEADD(DAY, -10, GETDATE())  AND LEN(content) = 6 AND ISNUMERIC(content) = 1 ");
            strSql.Append("GROUP BY  CONTENT , Username  ) t ) t1 ");
            strSql.Append("WHERE t1.id BETWEEN " + ((pageIndex - 1) * pageSize + 1).ToString() + " AND " + (pageIndex * pageSize).ToString() + "");

            DataTable dt = base.ExecuteDataTable(DatabaseEnum.Log4Net, CommandType.Text, strSql.ToString());

            return dt;

 二.cs

     public void CDataBind(int pageIndex) 
        {
            int pageSize = 5;//页大小
            int numCount = 0;//总记录数
            DataTable dt = null;
            try
            {
                dt = ControlInterfaceLogic.GetInterface().GetUserAndError(pageSize, pageIndex);
                DataView dv = new DataView(dt);
                DataRow[] drRowCount = dt.Select("id is not null");
                if (drRowCount.Length > 0)
                {
                    //总记录数
                    numCount = ControlInterfaceLogic.GetInterface().GetSumRow();

                    if (numCount > 0)
                    {
                        labCount.Text = numCount.ToString();
                        //当前页数
                        labPageIndex.Text = pageIndex.ToString();
                        if (numCount % pageSize == 0)
                        {
                            labPageCount.Text = (numCount / pageSize).ToString();
                        }
                        else
                        {
                            labPageCount.Text = (numCount / pageSize + 1).ToString();
                        }

                        divPage.Visible = true;
                        //数据库绑定
                        rptCErrorList.DataSource = dv;
                        rptCErrorList.DataBind();

                        rptFErrorList.DataSource = dv;
                        rptFErrorList.DataBind();
                        SetPageControl(pageIndex);
                    }
                    else
                    {
                        divPage.Visible = false;
                        rptCErrorList.DataSource = null;
                        rptCErrorList.DataBind();
                        rptFErrorList.DataSource = null;
                        rptFErrorList.DataBind();
                    }
                }
          
            }
            catch (Exception)
            {
                throw;
            }
        }

 设置分页控件可用

 void SetPageControl(int pageIndex)
        {
            if (pageIndex == 1 && pageIndex == int.Parse(labPageCount.Text))
            {
                lkFristPage.Enabled = lkBeforePage.Enabled = false;
                lkNextPage.Enabled = lkLastPage.Enabled = false;
                lkGo.Enabled = false;
            }
            else if (pageIndex == 1)
            {
                lkFristPage.Enabled = lkBeforePage.Enabled = false;
                lkNextPage.Enabled = lkLastPage.Enabled = true;
            }
            else if (pageIndex == int.Parse(labPageCount.Text))
            {
                lkFristPage.Enabled = lkBeforePage.Enabled = true;
                lkNextPage.Enabled = lkLastPage.Enabled = false;
            }
            else
            {
                lkFristPage.Enabled = lkBeforePage.Enabled = true;
                lkNextPage.Enabled = lkLastPage.Enabled = true;
            }
        }

 分页

   #region 分页
        //首页
        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lkFristPage_Click(object sender, EventArgs e)
        {
            CDataBind(1);
        }
        //上一页
        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lkBeforePage_Click(object sender, EventArgs e)
        {
            int pageIndex = int.Parse(labPageIndex.Text);
            pageIndex--;
            if (pageIndex > 0)
            {
                CDataBind(pageIndex);
            }
        }

        //下一页
        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lkNextPage_Click(object sender, EventArgs e)
        {
            int pageIndex = int.Parse(labPageIndex.Text);
            pageIndex++;
            if (pageIndex <= int.Parse(labPageCount.Text))
            {
                CDataBind(pageIndex);
            }
        }

        //尾页
        /// <summary>
        /// 尾页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lkLastPage_Click(object sender, EventArgs e)
        {
            CDataBind(int.Parse(labPageCount.Text));
        }

        //页面跳转 
        /// <summary>
        /// 页面跳转 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lkGo_Click(object sender, EventArgs e)
        {
            int pageIndex = int.Parse(txtPage.Value.Trim());

            CDataBind(pageIndex);
        }
        #endregion

 前台

<script type="text/javascript">
        function checkForm() {
            var re = /^\d+(?=\{1,9}\d+$|$)/
            var pageIndex = document.getElementById("txtPage").value;
            if (pageIndex == "") {
                alert("请输入页码!");
                return false;
            }
            if (!re.test(pageIndex)) {
                alert("页码须为整数!");
                document.getElementById("txtPage").value = '';
                return false;
            }

            if (pageIndex > Number(document.getElementById("labPageCount").textContent)) {
                alert("请输入小于等于总页码的页数!");
                document.getElementById("txtPage").value = '';
                return false;
            }
        }
    </script>

 页面

  <div style="width: 81%; height: 100px; margin: 10px auto;" id="divPage" runat="server">
                                <div style="width: 30%; float: left;">
                                    记录总数:<asp:Label runat="server" ID="labCount" Text="100"></asp:Label>
                                    条 页次<asp:Label runat="server" ID="labPageIndex" Text="1"></asp:Label>/<asp:Label runat="server" ID="labPageCount" Text="47"></asp:Label>
                                </div>
                                <div style="width: 60%; float: right; text-align: right;">
                                    <asp:LinkButton ID="lkFristPage" runat="server" OnClick="lkFristPage_Click">首页</asp:LinkButton>
                                    <asp:LinkButton ID="lkBeforePage" runat="server" OnClick="lkBeforePage_Click">上一页</asp:LinkButton>
                                    <asp:LinkButton ID="lkNextPage" runat="server" OnClick="lkNextPage_Click">下一页</asp:LinkButton>
                                    <asp:LinkButton ID="lkLastPage" runat="server" OnClick="lkLastPage_Click">尾页</asp:LinkButton>
                                    <input id="txtPage" type="text" style="width: 30px;" runat="server" />
                                    <asp:LinkButton ID="lkGo" runat="server" OnClick="lkGo_Click" OnClientClick="return checkForm()">GO</asp:LinkButton>
                                </div>
                            </div>

 








                                      没错,啥都没有。。

posted on 2013-07-13 16:31  dr_twilight  阅读(203)  评论(0编辑  收藏  举报