GridView分页技术

在界面上拖一个GridView控件,在PagerTemplate里放入如下代码:

第<asp:Label id="lblPageIndex" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1   %>' />页
                                 共/<asp:Label id="lblPageCount" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageCount   %>' />页
                                 <asp:linkbutton id="btnFirst" runat="server" causesvalidation="False" commandargument="First" commandname="Page" text="首页" />
                               <asp:linkbutton id="btnPrev" runat="server" causesvalidation="False" commandargument="Prev" commandname="Page" text="上一页" />
                              <asp:linkbutton id="btnNext" runat="server" causesvalidation="False" commandargument="Next" commandname="Page" text="下一页" />                         
                              <asp:linkbutton id="btnLast" runat="server" causesvalidation="False" commandargument="Last" commandname="Page" text="尾页" />                                           
                              <asp:textbox id="txtNewPageIndex" runat="server" width="20px" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1   %>' />
                              <asp:linkbutton id="btnGo" runat="server" causesvalidation="False" commandargument="-1" commandname="Page" text="GO" />

请注意:要想使用GridView内置的方法,commandname必须为Page,commandargument必须为对应的First,Prev,Next,Last,以及-1。

设置这些之后,我们只需要写少量的代码。当然GridView属性中也要设置AllowPaging="True",onpageindexchanging="GridView1_PageIndexChanging" PageSize="2"

接下来编写事件。

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                MyBind();
            }
        }
        private void MyBind()
        {
            DataTable dtParam = GetAllDeptInfo();
            GridView1.DataSource = dtParam;
            GridView1.DataBind();
        }
        public DataTable GetAllDeptInfo()
        {
            string connstr = "data source=localhost;Integrated Security=SSPI;initial catalog=Study";
            SqlConnection conn = new SqlConnection(connstr);
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            SqlCommand com = conn.CreateCommand();
            string sql = "select * from DeptInfo";
            DataTable myTable = new DataTable();
            com.CommandText = sql;
            SqlDataAdapter sda = new SqlDataAdapter(com);
            sda.Fill(myTable);
            if (conn.State == ConnectionState.Open) conn.Close();
            return myTable;
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView gvTemp = sender as GridView;
            int nowpage = 0;
            nowpage = e.NewPageIndex;
            if (nowpage == -2)
            {
                GridViewRow gvr = gvTemp.BottomPagerRow;
                if (gvr != null)
                {
                    TextBox tb =gvr.FindControl("txtNewPageIndex") as TextBox;
                    if (tb != null)
                    {
                        nowpage = int.Parse(tb.Text) - 1;
                    }
                }
            }
            nowpage=nowpage < 0 ? 0 : nowpage;
            nowpage = nowpage > gvTemp.PageCount - 1 ? gvTemp.PageCount : nowpage;
            gvTemp.PageIndex = nowpage;
            MyBind();
        }

OK,搞定了。

原文地址:https://www.cnblogs.com/zhangsongshan/p/2352575.html