天天看點

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

}