實現分頁功能(三層)

------------------------------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>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章