zoukankan      html  css  js  c++  java
  • 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,搞定了。

  • 相关阅读:
    linux下查找文件并按时间顺序排序的方法
    动态口令(OTP)认证技术概览
    [转]关于OpenSSL支持USBKEY证书的尝试
    关于CSP通过CpSetKeyParam存入证书相关问题
    Windows AntiDebug Reference
    cryptapi制作证书
    [转]国密SM3杂凑算法与实现
    [转]国密SM2非对称算法与实现
    [转]国密SM4对称算法实现说明(原SMS4无线局域网算法标准)
    证书的申请过程(usbkey)
  • 原文地址:https://www.cnblogs.com/zhangsongshan/p/2352575.html
Copyright © 2011-2022 走看看