使用分页模板完成,显示上一页,下一页,第一页,最后一页,和当前第x页,共y页 。

时间:2021-09-21 23:29:44
博客内容列表前台:
<asp:GridView ID="GridView2" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False" DataKeyNames="sid" 
        onrowdatabound="GridView2_RowDataBound" PageSize="3" 
        onpageindexchanging="GridView1_PageIndexChanging" ShowFooter="True">
        <Columns>
            <asp:BoundField DataField="sid" HeaderText="sid" InsertVisible="False" 
                ReadOnly="True" SortExpression="sid" />
            <asp:BoundField DataField="sname" HeaderText="sname" SortExpression="sname" />
            <asp:BoundField DataField="classid" HeaderText="classid" 
                SortExpression="classid" />
            <asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />
            <asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
            <asp:CheckBoxField DataField="isking" HeaderText="isking" 
                SortExpression="isking" />
            <asp:BoundField DataField="photo" HeaderText="photo" SortExpression="photo" />
        </Columns>
        <PagerSettings FirstPageText="|&amp;lt;" LastPageText="&amp;gt;|" 
            Mode="NextPreviousFirstLast" Visible="False" />
        <PagerTemplate>
            <asp:Button ID="Button1" runat="server" CommandArgument="First" 
                CommandName="page" Text="|&lt;" />
            &nbsp;<asp:Button ID="Button2" runat="server" CommandArgument="prev" 
                CommandName="page" Text="&lt;" />
            &nbsp;<asp:Button ID="Button3" runat="server" CommandArgument="next" 
                CommandName="page" Text="&gt;" />
            &nbsp;<asp:Button ID="Button4" runat="server" CommandArgument="Last" 
                CommandName="page" Text="&gt;|" />
            &nbsp;<asp:Label ID="Label1" runat="server"></asp:Label>
        </PagerTemplate>
    </asp:GridView>
    <br />
    <br />

    <asp:Button ID="btnFirst" runat="server" onclick="btnFirst_Click" Text="第一页" />
&nbsp;<asp:Button ID="btnPrev" runat="server" onclick="btnPrev_Click" Text="上一页" />
&nbsp;<asp:Button ID="btnNext" runat="server" onclick="btnNext_Click" Text="下一页" />
&nbsp;<asp:Button ID="btnLast" runat="server" onclick="btnLast_Click" Text="末页" />
&nbsp;<asp:Label ID="Label2" runat="server"></asp:Label>



后台:

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

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

  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();
        }

        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
          
                Label2.Text = "第" + (GridView2.PageIndex + 1).ToString() + "页,共" + GridView2.PageCount + "页";
      

        }

-------------------------------------------------------------------------------------------------

Web.config:
    <connectionStrings>
        <add name="studentConnectionString" connectionString="Data Source=PC_THINK-THINK;Initial Catalog=student;Persist Security Info=True;User ID=sa;Password=111111"
            providerName="System.Data.SqlClient" />
        <add name="studentConnectionString2" connectionString="Data Source=PC_THINK-THINK;Initial Catalog=student;Persist Security Info=True;User ID=sa;Password=111111"
            providerName="System.Data.SqlClient" />
    </connectionStrings>



SQLHelper.cs:
    public static DataTable ExecuteDataTable(string sql, params SqlParameter[] pms)
    {
        DataTable dt = new DataTable();
        SqlDataAdapter adapter = new SqlDataAdapter(sql,connStr);
        if (pms != null)
        {
            adapter.SelectCommand.Parameters.AddRange(pms);
        }
        adapter.Fill(dt);
        return dt;
    }


在这道题中用到了数据集 DataSet1.xsd



----------------------------------------------------------------------------------------------------