输入数字实现分页功能(三层)

本文个人在深圳游玩的时候突然想到的...这几周就有想写几篇关于输入数字的笔记,所以回家到之后就奋笔疾书的写出来发布了

    ------------------------------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" onclick="btnFirst_Click">第一页</asp:LinkButton>
        <asp:LinkButton
            ID="btnPre" runat="server" onclick="btnPre_Click">上一页</asp:LinkButton>
        <asp:LinkButton ID="btnNext"
                runat="server" onclick="btnNext_Click">下一页</asp:LinkButton>
        <asp:LinkButton ID="btnLast" runat="server" onclick="btnLast_Click">最后一页</asp:LinkButton><asp:TextBox
                    ID="txtIndex" runat="server"></asp:TextBox>
        <asp:LinkButton ID="btnGO" runat="server" onclick="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>

    

    输入和数字输入和数字

文章结束给大家分享下程序员的一些笑话语录: 人脑与电脑的相同点和不同点,人脑会记忆数字,电脑也会记忆数字;人脑会记忆程序,电脑也会记忆程序,但是人脑具有感知能力,这种能力电脑无法模仿,人的记忆会影响到人做任何事情,但是电脑只有程序软件。比尔还表示,人脑与电脑之间最重要的一个差别就是潜意识。对于人脑存储记忆的特别之处,比尔表示,人脑并不大,但是人脑重要的功能是联络,人脑会把同样的记忆存储在不同的地方,因此记忆读取的速度就不相同,而这种速度取决于使用的频率和知识的重要性。人脑的记忆存储能力会随着年龄增长而退化,同时记忆的质量也会随着年龄退化。经典语录网

原文地址:https://www.cnblogs.com/xinyuyuanm/p/3091800.html