天天看点

GridView的PagerTemplate分页

注意点:

1,一定要把AllowPaging设定为True,否则前台代码无显示。

2,再写一个执行的PageIndexChanging事件

3,Gridview分页数应该足够,否则比如设定10行/页,但是数据只有10行以下,比如8行,则“分页控件”则不显示(刚开始以为哪里设定错误了):

GridView的PagerTemplate分页

前台代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

        AllowPaging="true" onpageindexchanging="GridView1_PageIndexChanging"

        >

        <Columns>

            <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" />

            <asp:BoundField DataField="Equipment_Name" HeaderText="Equipment_Name" />

            <asp:BoundField DataField="Cal_Date" HeaderText="Cal_Date" />

            <asp:BoundField DataField="Due_Date" HeaderText="Due_Date" />

            <asp:BoundField DataField="Status" HeaderText="Status" />

            <asp:BoundField DataField="Remark" HeaderText="Remark" />

        </Columns>

<PagerTemplate>

                                    当前第:

<asp:Label  ID="LabelCurrentPage" runat="server"

 Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>

 页/共:

<asp:Label ID="LabelPageCount" runat="server"

 Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label> 页

 <asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"

 Visible='<%#((GridView)Container.NamingContainer).PageIndex != 0 %>'>首页</asp:LinkButton>

<asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page"

 Visible='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'>上一页</asp:LinkButton>

<asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"

 Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>下一页</asp:LinkButton>

<asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"

 Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>尾页</asp:LinkButton>

  转到第

<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" />

                                    </PagerTemplate>

    </asp:GridView>

后台代码:

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

public partial class Cal_calAdd : System.Web.UI.Page

{

    public SqlConnection GetConnection()

    {

        string myStr = ConfigurationManager.AppSettings["calConnectionString"].ToString();

        SqlConnection myConn = new SqlConnection(myStr);

        return myConn;

    }

    protected void Page_Load(object sender, EventArgs e)

        if (!IsPostBack)

        {

            GridViewBind();

        }

    protected void GridViewBind()

        SqlConnection con = GetConnection();

        con.Open();

        string sql = "SELECT ID,Equipment_Name,Cal_Date,Due_Date,Status,Remark FROM calMainRecords";

        SqlDataAdapter sda = new SqlDataAdapter(sql, con);

        DataSet ds = new DataSet();

        sda.Fill(ds);

        this.GridView1.DataSource = ds;

        this.GridView1.DataBind();

        con.Close();

    protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)

        GridViewBind();

    protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)

    protected void DetailsView1_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

        GridView theGrid = sender as GridView; // refer to the GridView

        int newPageIndex = 0;

        if (-2 == e.NewPageIndex)

        { // when click the "GO" Button

            TextBox txtNewPageIndex = null;

            //GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate

            GridViewRow pagerRow = theGrid.BottomPagerRow; //GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow

            if (null != pagerRow)

            {

                txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value

            }

            if (null != txtNewPageIndex)

                newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex

        else

        { // when click the first, last, previous and next Button

            newPageIndex = e.NewPageIndex;

        // check to prevent form the NewPageIndex out of the range

        newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;

        newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

        // specify the NewPageIndex

        theGrid.PageIndex = newPageIndex;

        this.GridViewBind();

}