GridView 自訂分頁
http://dotnetmis91.blogspot.com/2008/08/gridview_20.html

本文為參考坎尼大大的文章 做成的快速製作版 如果趕時間可以馬上完成的步驟紀錄

1. 自訂Pager Templete, 將相關物件拉進Pager 或直接貼程式碼:  注意程式碼的部分已經將Click的程序名稱掛上去 若 是自己用啦的請注意程序名稱,文字部分可自行換成圖片 直接加在</Columns> 或 <AlternatingRowStyle>  下,在</asp:GridView>內。
<PagerTemplate>
            <asp:Label ID="Label1" runat="server" ></asp:Label>
            <asp:LinkButton ID="lbtnfirst" runat="server" CommandArgument="first" OnClick="Query_Click" >First</asp:LinkButton>
            <asp:LinkButton ID="lbtnPrevious" runat="server" CommandArgument="previous" OnClick="Query_Click" >Previous</asp:LinkButton>
            <asp:LinkButton ID="lbtnnext" runat="server" CommandArgument="next" OnClick="Query_Click" >Next</asp:LinkButton>
            <asp:LinkButton ID="lbtnlast" runat="server" CommandArgument="last" OnClick="Query_Click">Last</asp:LinkButton>
            <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
            </asp:DropDownList>
            <asp:Label ID="Label2" runat="server" ></asp:Label>


2. 貼入相關C#程序,若需要相關說明請參考坎尼大大的文章:
    protected void Query_Click(object sender, EventArgs e) {
        int intPageIndex = 0;
        LinkButton lkbtn = (LinkButton)sender;
        switch (lkbtn.CommandArgument.ToString()) {
            case "previous":
                if (GridView1.PageIndex > 0) {
                    intPageIndex = GridView1.PageIndex - 1;
                } break;
            case "next":
                if (GridView1.PageIndex < GridView1.PageCount - 1) {
                    intPageIndex = GridView1.PageIndex + 1;
                } break;
            case "last":
                intPageIndex = GridView1.PageCount - 1;
                break;   
        }
        GridView1.PageIndex = intPageIndex;
    }

   

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddlPage = ((DropDownList)GridView1.BottomPagerRow.FindControl("DropDownList1"));
        GridView1.PageIndex = ddlPage.SelectedIndex;
    }

    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        pageSet(GridView1);
    }
    public void pageSet(object gvQuery) {
        GridView gv = (GridView)gvQuery;
        Label lbCount = (Label)gv.BottomPagerRow.FindControl("Label1");
        Label lbPage = (Label)gv.BottomPagerRow.FindControl("Label2");
        DropDownList ddlJump = (DropDownList)gv.BottomPagerRow.FindControl("DropDownList1");

        lbCount.Text = "共 " + (gv.PageSize * gv.PageCount).ToString() + " 筆"; // 要精準的部份需另外判斷!
        lbPage.Text = "共 " + gv.PageCount + " 頁";

        for (int i = 1; i <= gv.PageCount; i++) {
            ddlJump.Items.Add(new ListItem(i.ToString()));

            if (gv.PageIndex == 0) {
                ((LinkButton)gv.BottomPagerRow.FindControl("lbtnfirst")).Enabled = false;
                ((LinkButton)gv.BottomPagerRow.FindControl("lbtnPrevious")).Enabled = false;
            }
            else if (gv.PageIndex == (gv.PageCount - 1)) {
                ((LinkButton)gv.BottomPagerRow.FindControl("lbtnnext")).Enabled = false;
                ((LinkButton)gv.BottomPagerRow.FindControl("lbtnlast")).Enabled = false;
            }
            ddlJump.SelectedIndex = gv.PageIndex;
           
        }
    }


3. 設定GridView 相關屬性或直接加上程式碼部分加上觸發事件部份

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            CellPadding="4" DataKeyNames="id" DataSourceID="SqlDataSource1" ForeColor="#333333"
              OnDataBound="GridView1_DataBound">

創作者介紹
創作者 andyyu0920 的頭像
andyyu0920

ANdyYoU_蹤雁_數位記錄本

andyyu0920 發表在 痞客邦 留言(2) 人氣()


留言列表 (2)

發表留言
  • 訪客
  • 很棒的文章,還搞不太懂gridview..受益良多
  • w309907
  • s4QvyDK1:1大牌專賣,兔毛,兔毛這個也太可愛了吧,專櫃新貨,咨詢請加LINE:bv666, 貨到付款,黑貓宅配喔