zoukankan      html  css  js  c++  java
  • GridView手写数据源并实现分页效果

    前台代码:

    <asp:GridView ID="GridView2" runat="server" AllowPaging="True" 
                AllowSorting="True" onpageindexchanging="GridView1_PageIndexChanging" 
                onrowdatabound="GridView1_RowDataBound" PageSize="3" ShowFooter="True" 
                CellPadding="4" ForeColor="#333333" GridLines="None" 
               >
                <AlternatingRowStyle BackColor="White" />
                <EditRowStyle BackColor="#2461BF" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerSettings Visible="False" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <PagerTemplate>
                    <asp:Button ID="Button1" runat="server" CommandArgument="first" 
                        CommandName="page" Text="|&lt;" Width="30px" />
                    <asp:Button ID="Button2" runat="server" CommandArgument="prev" 
                        CommandName="page" Text="&lt;" Width="30px" />
                    <asp:Button ID="Button3" runat="server" CommandArgument="next" 
                        CommandName="page" Text="&gt;" Width="30px" />
                    <asp:Button ID="Button4" runat="server" CommandArgument="last" 
                        CommandName="page" Text="&gt;|" Width="30px" />
                    <asp:Label ID="Label1" runat="server"></asp:Label>
                </PagerTemplate>
                <RowStyle BackColor="#EFF3FB" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#F5F7FB" />
                <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                <SortedDescendingCellStyle BackColor="#E9EBEF" />
                <SortedDescendingHeaderStyle BackColor="#4870BE" />
            </asp:GridView>


            <br />
            <asp:Button ID="btnFirst" runat="server" onclick="btnFirst_Click" Text="第一页" />
            <asp:Button ID="btnPrev" runat="server" onclick="btnPrev_Click" Text="上一页" />
            <asp:Button ID="btnNext" runat="server" onclick="btnNext_Click" Text="下一页" />
            <asp:Button ID="btnLast" runat="server" onclick="btnLast_Click" Text="最后一页" />

    后台代码:

        protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindStudent1();
                }
            }

        private void BindStudent1()
            {
                string sql = "select * from student";
                DataTable dt = SqlHelp.ExecuteDataTable(sql);
                GridView2.DataSource = dt;
                GridView2.DataBind();
            }

            protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                if (e.NewPageIndex >= 0)
                {
                    GridView1.PageIndex = e.NewPageIndex;
                    this.BindStudent();
                }
            }


            protected void btnFirst_Click(object sender, EventArgs e)
            {
                this.GridView2.PageIndex = 0;
                this.BindStudent1();
            }


            protected void btnPrev_Click(object sender, EventArgs e)
            {
                int index = GridView2.PageIndex;
                if (index >= 1)
                {
                    index--;
                }
                GridView2.PageIndex = index;
                this.BindStudent1();
            }


            protected void btnNext_Click(object sender, EventArgs e)
            {
                int index = GridView2.PageIndex;
                if (index<this.GridView2.PageCount+1)
                {
                    index++;
                }
                GridView2.PageIndex = index;
                this.BindStudent1();
            }


            protected void btnLast_Click(object sender, EventArgs e)
            {
                this.GridView2.PageIndex = this.GridView2.PageCount - 1;
                this.BindStudent1();
            }

  • 相关阅读:
    JS写游戏
    为运算表达式设计优先级
    原子的数量
    二叉搜索树的范围和
    所有可能的满二叉树
    有效的井字游戏
    站在对象模型的尖端
    执行期语意学
    构造、析构、拷贝语意学
    [CSP-S模拟测试]:序列(二分答案+树状数组)
  • 原文地址:https://www.cnblogs.com/qiqiBoKe/p/2806173.html
Copyright © 2011-2022 走看看