GridView 사용자 지정 페이지

7362 단어 GridView
Gridview에 추가
이런 PagerTemplate

                          <PagerTemplate>
                                   <div id="main">
                                      <div id="info">&nbsp;&nbsp;  :<asp:Label ID="lblPageCurrent" runat="server" Text="1" CssClass="txtInfo"></asp:Label>
                                      /<asp:Label ID="lblPageCount" runat="server" Text="1"></asp:Label>&nbsp;&nbsp;
                                       &nbsp;<asp:Label ID="lblPageRow" runat="server" Text="1" CssClass="txtInfo"></asp:Label>&nbsp;   
                                      </div>
                                      <div id="page"> 
                                          <asp:LinkButton ID="btnFirst" runat="server" CssClass="link" CommandName="Pager" CommandArgument="First" OnCommand="NavigateToPage">[  ]</asp:LinkButton>&nbsp;
                                          <asp:LinkButton ID="btnPrev" runat="server" CssClass="link" CommandName="Pager" CommandArgument="Prev" OnCommand ="NavigateToPage">[   ]</asp:LinkButton>&nbsp;
                                          <asp:LinkButton ID="btnNext" runat="server" CssClass="link" CommandName="Pager" CommandArgument="Next" OnCommand="NavigateToPage">[   ]</asp:LinkButton>&nbsp;
                                          <asp:LinkButton ID="btnLast" runat="server" CssClass="link" CommandName="Pager" CommandArgument="Last" OnCommand="NavigateToPage">[  ]</asp:LinkButton>&nbsp;&nbsp;
                                          &nbsp;&nbsp;   :<asp:DropDownList ID="DdlGoPage" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DdlGoPage_SelectedIndexChanged" SkinID="ddlSkin"></asp:DropDownList>
                                      </div>
                                   </div>
                             </PagerTemplate> 

백엔드에 이 컨트롤 페이지 코드를 넣으세요

#region//    

    /***************         ****************/
    int rowCount = 0;
    /*************************************************/

    protected void NavigateToPage(object sender, CommandEventArgs e)
    {
        //    
        switch (e.CommandArgument.ToString())
        {
            case "First":
                this.gvMail.PageIndex = 0;
                break;
            case "Prev":
                if (this.gvMail.PageIndex > 0)
                    this.gvMail.PageIndex -= 1;
                break;
            case "Next":
                if (this.gvMail.PageIndex < (this.gvMail.PageCount - 1))
                    this.gvMail.PageIndex += 1;
                break;
            case "Last":
                this.gvMail.PageIndex = this.gvMail.PageCount - 1;
                break;
        }
        BindPageData();
    } 

    protected void gvMail_DataBound(object sender, EventArgs e)
    {
        GridViewRow pagerRow = gvMail.BottomPagerRow;
        if (pagerRow != null) //        ,     null         ,     
        {
            //  Label  ,      
            Label lblCurrent = (Label)pagerRow.Cells[0].FindControl("lblPageCurrent");
            Label lblCount = (Label)pagerRow.Cells[0].FindControl("lblPageCount");
            Label lblRow = (Label)pagerRow.Cells[0].FindControl("lblPageRow");
            //      ,         
            LinkButton btnFirstTem = (LinkButton)pagerRow.Cells[0].FindControl("btnFirst");
            LinkButton btnPrevTem = (LinkButton)pagerRow.Cells[0].FindControl("btnPrev");
            LinkButton btnNextTem = (LinkButton)pagerRow.Cells[0].FindControl("btnNext");
            LinkButton btnLastTem = (LinkButton)pagerRow.Cells[0].FindControl("btnLast");

            if (lblCurrent != null)
                lblCurrent.Text = (this.gvMail.PageIndex + 1).ToString();
            if (lblCount != null)
                lblCount.Text = this.gvMail.PageCount.ToString();
            if (lblRow != null)
                lblRow.Text = rowCount.ToString();

            if (this.gvMail.PageIndex == 0)
            {
                btnFirstTem.Enabled = false;
                btnPrevTem.Enabled = false;
                //    ,         
                if (this.gvMail.PageCount == 1)
                {
                    btnNextTem.Enabled = false;
                    btnLastTem.Enabled = false;
                }
            }
            else if (this.gvMail.PageIndex == (this.gvMail.PageCount - 1))
            {
                btnNextTem.Enabled = false;
                btnLastTem.Enabled = false;
            }
        }

    }

    protected void gvMail_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Pager)
        {
            DropDownList ddl = (DropDownList)e.Row.FindControl("DdlGoPage");

            //       
            Mail mail = new Mail();
            DataSet mailDs = mail.GetMailCountByFloder(folderID);
            rowCount = Int32.Parse(mailDs.Tables[0].Rows[0][0].ToString());

            if (rowCount!=0)
            {
                int recordcount = rowCount;
                int pagecount = recordcount / gvMail.PageSize;
                for (int i = 1; i <=pagecount+1; i++)
                {
                    ddl.Items.Add(i.ToString());
                }
            }
            ddl.SelectedValue = (gvMail.PageIndex + 1).ToString();  
        }

    }

    //            
    protected void DdlGoPage_SelectedIndexChanged(object sender, EventArgs e)
    {
        int index = int.Parse(((DropDownList)sender).SelectedValue) - 1;
        //ScriptManager.RegisterStartupScript(Page, this.GetType(), "sucess", "<script>alert('" + index + "');</script>", false);
        gvMail.PageIndex = index;
        BindPageData();
    }
    #endregion 

실제 상황에 따라 GridView 이름 바꾸기
주의 gvUserRowCreated 이벤트 중 하나의 조회 기록 총수는 구체적인 상황에 따라 상응하는 변화를 해야 한다
프런트의 Gridview에 이 두 이벤트 속성을 추가합니다

OnDataBound="gvMail_DataBound" OnRowCreated="gvMail_RowCreated"

여기 이름이 백스테이지 이름에 맞춰서 바뀔 수 있어요.
마지막으로 GridView의 AllowPaging을 True로 설정하는 것을 잊지 마세요.

좋은 웹페이지 즐겨찾기