GridView实现分页的两种方法

前台设计:

 

<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConflictDetection="CompareAllValues"
            ConnectionString="<%$ ConnectionStrings:studentConnectionString %>"
            DeleteCommand="DELETE FROM [student] WHERE [sid] = @original_sid AND [sname] = @original_sname AND (([classid] = @original_classid) OR ([classid] IS NULL AND @original_classid IS NULL)) AND (([sex] = @original_sex) OR ([sex] IS NULL AND @original_sex IS NULL)) AND (([age] = @original_age) OR ([age] IS NULL AND @original_age IS NULL)) AND [isking] = @original_isking AND (([photo] = @original_photo) OR ([photo] IS NULL AND @original_photo IS NULL))"
            InsertCommand="INSERT INTO [student] ([sname], [classid], [sex], [age], [isking], [photo]) VALUES (@sname, @classid, @sex, @age, @isking, @photo)"
            OldValuesParameterFormatString="original_{0}"
            SelectCommand="SELECT * FROM [student]"
            UpdateCommand="UPDATE [student] SET [sname] = @sname, [classid] = @classid, [sex] = @sex, [age] = @age, [isking] = @isking, [photo] = @photo WHERE [sid] = @original_sid AND [sname] = @original_sname AND (([classid] = @original_classid) OR ([classid] IS NULL AND @original_classid IS NULL)) AND (([sex] = @original_sex) OR ([sex] IS NULL AND @original_sex IS NULL)) AND (([age] = @original_age) OR ([age] IS NULL AND @original_age IS NULL)) AND [isking] = @original_isking AND (([photo] = @original_photo) OR ([photo] IS NULL AND @original_photo IS NULL))">
            <DeleteParameters>
                <asp:Parameter Name="original_sid" Type="Int32" />
                <asp:Parameter Name="original_sname" Type="String" />
                <asp:Parameter Name="original_classid" Type="Int32" />
                <asp:Parameter Name="original_sex" Type="String" />
                <asp:Parameter Name="original_age" Type="Byte" />
                <asp:Parameter Name="original_isking" Type="Boolean" />
                <asp:Parameter Name="original_photo" Type="String" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="sname" Type="String" />
                <asp:Parameter Name="classid" Type="Int32" />
                <asp:Parameter Name="sex" Type="String" />
                <asp:Parameter Name="age" Type="Byte" />
                <asp:Parameter Name="isking" Type="Boolean" />
                <asp:Parameter Name="photo" Type="String" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="sname" Type="String" />
                <asp:Parameter Name="classid" Type="Int32" />
                <asp:Parameter Name="sex" Type="String" />
                <asp:Parameter Name="age" Type="Byte" />
                <asp:Parameter Name="isking" Type="Boolean" />
                <asp:Parameter Name="photo" Type="String" />
                <asp:Parameter Name="original_sid" Type="Int32" />
                <asp:Parameter Name="original_sname" Type="String" />
                <asp:Parameter Name="original_classid" Type="Int32" />
                <asp:Parameter Name="original_sex" Type="String" />
                <asp:Parameter Name="original_age" Type="Byte" />
                <asp:Parameter Name="original_isking" Type="Boolean" />
                <asp:Parameter Name="original_photo" Type="String" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
            PageSize="3" onpageindexchanging="GridView1_PageIndexChanging"
            onrowdatabound="GridView1_RowDataBound" ShowFooter="True">
            <PagerSettings FirstPageText="|&amp;lt;" LastPageText="&amp;gt;|"
                Mode="NextPreviousFirstLast" />
            <PagerTemplate>
                <asp:Button ID="Button1" runat="server" CommandArgument="First"
                    CommandName="page" Text="|&lt;" Width="30px" />
                &nbsp;<asp:Button ID="Button2" runat="server" CommandArgument="prev"
                    CommandName="page" Text="&lt;" Width="30px" />
                &nbsp;<asp:Button ID="Button3" runat="server" CommandArgument="next"
                    CommandName="page" Text="&gt;" Width="30px" />
                &nbsp;<asp:Button ID="Button4" runat="server" CommandArgument="last"
                    CommandName="page" Text="&gt;|" Width="30px" />
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </PagerTemplate>
        </asp:GridView>
   
        <br />
        <br />
        <asp:GridView ID="GridView2" runat="server" AllowPaging="True"
            PageSize="3" onpageindexchanging="GridView1_PageIndexChanging"
            onrowdatabound="GridView1_RowDataBound" ShowFooter="True" Width="302px">
            <PagerSettings FirstPageText="|&amp;lt;" LastPageText="&amp;gt;|"
                Mode="NextPreviousFirstLast" Visible="False" />
            <PagerTemplate>
                <asp:Button ID="Button1" runat="server" CommandArgument="First"
                    CommandName="page" Text="|&lt;" Width="30px" />
                &nbsp;<asp:Button ID="Button2" runat="server" CommandArgument="prev"
                    CommandName="page" Text="&lt;" Width="30px" />
                &nbsp;<asp:Button ID="Button3" runat="server" CommandArgument="next"
                    CommandName="page" Text="&gt;" Width="30px" />
                &nbsp;<asp:Button ID="Button4" runat="server" CommandArgument="last"
                    CommandName="page" Text="&gt;|" Width="30px" />
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </PagerTemplate>
        </asp:GridView>
   
        <br />
        <asp:Button ID="btnfirst" runat="server" οnclick="btnfirst_Click" Text="第一页" />
&nbsp;
        <asp:Button ID="btnPrev" runat="server" οnclick="btnPrev_Click" Text="上一页" />
&nbsp;
        <asp:Button ID="btnNext" runat="server" οnclick="btnNext_Click" Text="下一页" />
&nbsp;
        <asp:Button ID="btnLast" runat="server" οnclick="btnLast_Click" Text="最后一页" />
   
    </div>
    </form>
</body>

 

 

后台设计:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


namespace _12_6内容
{
    public partial class Page1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindStudent();
            }
           
        }

        private void BindStudent()
        {
            string sql = "select * from student";
            DataTable dt = _11_28DataSet111.SqlHealper1.ExecuteDataTable(sql);
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();
            this.GridView2.DataSource = dt;
            this.GridView2.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Pager)
            {
                (e.Row.Cells[0].FindControl("Label1") as Label).Text = "第"+(GridView1.PageIndex+1).ToString()+"页,共"+GridView1.PageCount+"页";
            }
        }

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

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

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

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

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

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值