实现分页功能(三层)

17 篇文章 0 订阅

------------------------------DAL层:--------------------------

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataTable GetListDataTable(int PageSize, int PageIndex)
        {
            SqlParameter[] parameters = {

new SqlParameter("@PageSize", SqlDbType.Int),
new SqlParameter("@PageIndex", SqlDbType.Int)
};


            parameters[0].Value = PageSize;
            parameters[1].Value = PageIndex;
            return DbHelperSQL.RunProcedureDataTable("pro_fenye", parameters);
        }


--------------------BLL层:--------------------------

      public DataTable GetListDataTable(int pagesize, int pageindex)
        {
            return dal.GetListDataTable(pagesize, pageindex);
        }


------------------DbHelperSQL:-----------------------

        public static DataTable RunProcedureDataTable(string storedProcName, IDataParameter[] parameters)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                DataTable dt = new DataTable();
                connection.Open();
                SqlDataAdapter sqlDA = new SqlDataAdapter();
                sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
                sqlDA.Fill(dt);
                connection.Close();
                return dt;
            }
        }


---------------------WebForm1.aspx------------------------------

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
    <title></title>
    <script src="js/Jquery1.7.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $('#txtIndex').focus(function () {
                $(this).val('');
            })
        })
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            Height="369px" Width="978px">
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="编号" />
                <asp:BoundField DataField="NewsTitle" HeaderText="标题" />
                <asp:BoundField DataField="NewsContent" HeaderText="内容" />
                <asp:BoundField DataField="CreateTime" HeaderText="创建时间" />
            </Columns>
        </asp:GridView>
        <br />
    </div>
    <div>
        <asp:LinkButton ID="btnFirst" runat="server" οnclick="btnFirst_Click">第一页</asp:LinkButton>
        <asp:LinkButton
            ID="btnPre" runat="server" οnclick="btnPre_Click">上一页</asp:LinkButton>
        <asp:LinkButton ID="btnNext"
                runat="server" οnclick="btnNext_Click">下一页</asp:LinkButton>
        <asp:LinkButton ID="btnLast" runat="server" οnclick="btnLast_Click">最后一页</asp:LinkButton><asp:TextBox
                    ID="txtIndex" runat="server"></asp:TextBox>
        <asp:LinkButton ID="btnGO" runat="server" οnclick="btnGO_Click">GO</asp:LinkButton></div>
    </form>
</body>

</html>


----------------------WebForm1.aspx.cs------------------------------

namespace SanCengFenYe
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        int pagesize = 10;
        int pageindex = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["pageindex"] = 1;
                GetLastPageindex();
                LoadData();
            }
        }


        private void GetLastPageindex()
        {
            BLL.T_News1 bnews = new BLL.T_News1();
            int totalcount = bnews.GetRecordCount("");
            if (totalcount % pagesize == 0)
            {
                ViewState["lastpageindex"] = totalcount / pagesize;
            }
            else
            {
                ViewState["lastpageindex"] = totalcount / pagesize + 1;
            }
        }


        private void LoadData()
        {
            BLL.T_News1 bnews = new BLL.T_News1();
            DataTable dt = bnews.GetListDataTable(pagesize, Convert.ToInt32(ViewState["pageindex"]));
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();
        }


        protected void btnFirst_Click(object sender, EventArgs e)
        {
            ViewState["pageindex"] = 1;
            LoadData();
        }


        protected void btnPre_Click(object sender, EventArgs e)
        {
            int pageindex = Convert.ToInt32(ViewState["pageindex"]);
            if (pageindex>1)
            {
                pageindex--;
                ViewState["pageindex"] = pageindex;
                LoadData();
            }
        }


        protected void btnNext_Click(object sender, EventArgs e)
        {
            int pageindex = Convert.ToInt32(ViewState["pageindex"]);
            if (pageindex<Convert.ToInt32(ViewState["lastpageindex"]))
            {
                pageindex++;
                ViewState["pageindex"] = pageindex;
                LoadData();
            }
        }


        protected void btnLast_Click(object sender, EventArgs e)
        {
            ViewState["pageindex"] = ViewState["lastpageindex"];
            LoadData();
        }


        protected void btnGO_Click(object sender, EventArgs e)
        {
            int result;
            if (int.TryParse(txtIndex.Text, out result) == true)
            {
                ViewState["pageindex"] = txtIndex.Text.Trim();
                LoadData();
            }
            else
            {
                txtIndex.Text = "请输入合法的数字";
            }
        }
    }
}


---------------Web.config--------------

  <connectionStrings>
    <add name="constr" connectionString="data source=PC-20130106GGEO;initial catalog=News;user id=sa;pwd=111111"/>
  </connectionStrings>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值