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" οnclick="btnFirst_Click" Text="第一页" />
        <asp:Button ID="btnPrev" runat="server" οnclick="btnPrev_Click" Text="上一页" />
        <asp:Button ID="btnNext" runat="server" οnclick="btnNext_Click" Text="下一页" />
        <asp:Button ID="btnLast" runat="server" οnclick="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();
        }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值