控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

GridView和Repeater自定义分页功能的实现

作者:佚名 出处:互联网 2010年08月02日 阅读:

1、GridView自定义分页前台核心代码:

GridView.aspx

<PagerTemplate>
                <br />
                <asp:Label ID="lblPage" runat="server" Text='<%# "共" + ((GridView)Container.NamingContainer).PageCount + "页,当前第" + (((GridView)Container.NamingContainer).PageIndex + 1) + "页" %>'></asp:Label>
                <asp:LinkButton ID="lbtnFirst" runat="server" Text="首页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'
                    CommandName="Page" CommandArgument="First"></asp:LinkButton>
                <asp:LinkButton ID="lbtnPrev" runat="server" Text="上一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'
                    CommandName="Page" CommandArgument="Prev"></asp:LinkButton>
                <asp:PlaceHolder ID="numeric" runat="server"></asp:PlaceHolder>
                <asp:LinkButton ID="lbtnNext" runat="server" Text="下一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>'
                    CommandName="Page" CommandArgument="Next"></asp:LinkButton>
                <asp:LinkButton ID="lbtnLast" runat="server" Text="尾页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>'
                    CommandName="Page" CommandArgument="Last"></asp:LinkButton>
                <asp:DropDownList ID="ddlJumpPage" runat="server">
                </asp:DropDownList>
                <asp:ImageButton ID="btnGo" runat="server" Width="30px" Height="20px" ImageAlign="Bottom" ImageUrl="~/images/go.jpg" CommandName="Go" />
            </PagerTemplate>
&nbsp;

1、GridView自定义分页前台核心代码:

GridView.aspx

//根据命令来变更当前页码
    protected void gvTest_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Page")
        {
            switch (e.CommandArgument.ToString())
            {
                case "First":
                    gvTest.PageIndex = 0;
                    break;
                case "Prev":
                    if (gvTest.PageIndex > 0)
                    {
                        gvTest.PageIndex -= 1;
                    }
                    break;
                case "Next":
                    if (gvTest.PageIndex < (gvTest.PageCount - 1))
                    {
                        gvTest.PageIndex += 1;
                    }
                    break;
                case "Last":
                    gvTest.PageIndex = gvTest.PageCount - 1;
                    break;
                default:
                    gvTest.PageIndex = Int32.Parse(e.CommandArgument.ToString()) - 1;
                    break;
            }
        }
        else
        {
            if (e.CommandName == "Go")
            {
                DropDownList ddlJumpPage = (DropDownList)gvTest.BottomPagerRow.FindControl("ddlJumpPage");
                int index = Int32.Parse(ddlJumpPage.SelectedValue);
                gvTest.PageIndex = index - 1;
                GetData();
            }
        }
    }

    //显示页码导航
    protected void gvTest_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Pager)
        {
            PlaceHolder ph = (PlaceHolder)e.Row.FindControl("numeric");
            int startIndex = (gvTest.PageIndex) / 10 * 10 + 1;//根据当前页得到第一个页码
            int endIndex = startIndex + 9;//显示10个页码
            if (endIndex > gvTest.PageCount)//判断是否为最后一页
            {
                endIndex = gvTest.PageCount;
            }
            for (int i = startIndex; i <= endIndex; i++)//通过循环动态显示页码
            {
                var lbtn = new LinkButton { CommandName = "Page", CommandArgument = i.ToString(), Text = "[" + i + "]" };
                ph.Controls.Add(i - 1 != gvTest.PageIndex ? (Control)lbtn : new LiteralControl(i.ToString()));
                ph.Controls.Add(new LiteralControl(" "));
            }
        }
    }
&nbsp;

3、Repeater自定义分页前台核心代码

Repeater.aspx

<asp:Repeater ID="rpTest" runat="server" OnItemDataBound="rpTest_ItemDataBound" OnItemCreated="rpTest_ItemCreated">

...

<FooterTemplate>
                <tr>
                    <td colspan="6">
                        <br />
                        共<asp:Label ID="lblPageCount" runat="server" Text=""></asp:Label>页,当前第<asp:Label
                            ID="lblCurrentPage" runat="server" Text=""></asp:Label>页
                        <asp:HyperLink ID="hlFirst" runat="server" Text="首页"></asp:HyperLink>
                        <asp:HyperLink ID="hlPrev" runat="server" Text="上一页"></asp:HyperLink>
                        <asp:PlaceHolder ID="numeric" runat="server"></asp:PlaceHolder>
                        <asp:HyperLink ID="hlNext" runat="server" Text="下一页"></asp:HyperLink>
                        <asp:HyperLink ID="hlLast" runat="server" Text="尾页"></asp:HyperLink>
                        <asp:DropDownList ID="ddlJumpPage" runat="server">
                        </asp:DropDownList>
                        <asp:ImageButton ID="ibtnGo" runat="server" Width="30px" Height="20px" ImageAlign="Bottom"
                            ImageUrl="~/images/go.jpg" OnClick="ibtnGo_Click" />
                    </td>
                </tr>
                </table>
            </FooterTemplate>
</asp:Repeater>
<asp:Label ID="lblMessage" runat="server"></asp:Label>

4、Repeater自定义分页后台核心代码

Repeater.aspx.cs

//返回待绑定的数据源
    private PagedDataSource GetPds()
    {
        StudentInfo stuInfo = new StudentInfo();
        DataSet ds = stuInfo.GetStudent();
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = ds.Tables[0].DefaultView;//设置数据源
        pds.AllowPaging = true;//允许分页
        pds.PageSize = 10;//一页显示10条信息
        if (Request.QueryString["page"] != null)//判断页面传值是否为空
        {
            pds.CurrentPageIndex = Int32.Parse(Request.QueryString["page"].ToString()) - 1;
        }
        else
        {
            pds.CurrentPageIndex = 0;
        }
        return pds;
    }

 

//显示底部分页按钮并向下拉列表控件中填充数据
    protected void rpTest_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (GetPds().DataSourceCount != 0)
        {
            if (e.Item.ItemType == ListItemType.Footer)//判断是否为页脚
            {
                Label lblRecordCount = (Label)e.Item.FindControl("lblRecordCount");
                Label lblPageCount = (Label)e.Item.FindControl("lblPageCount");
                Label lblCurrentPage = (Label)e.Item.FindControl("lblCurrentPage");
                HyperLink hlFirst = (HyperLink)e.Item.FindControl("hlFirst");
                HyperLink hlPrev = (HyperLink)e.Item.FindControl("hlPrev");
                HyperLink hlNext = (HyperLink)e.Item.FindControl("hlNext");
                HyperLink hlLast = (HyperLink)e.Item.FindControl("hlLast");
                DropDownList ddlJumpPage = (DropDownList)e.Item.FindControl("ddlJumpPage");
                int pageCount = GetPds().PageCount;
                int curPage = GetPds().CurrentPageIndex + 1;
                for (int i = 1; i <= pageCount; i++)
                {
                    ddlJumpPage.Items.Add(i.ToString());
                }
                if (pageCount == 1)//若总页数只有一页,则底部分页链接不可用
                {
                    hlFirst.Enabled = false;
                    hlPrev.Enabled = false;
                    hlNext.Enabled = false;
                    hlLast.Enabled = false;
                }
                else
                {
                    if (curPage <= 1)//若当前为首页,则首页和上一页的链接不可用
                    {
                        hlFirst.Enabled = false;
                        hlPrev.Enabled = false;
                        hlNext.Enabled = true;
                        hlLast.Enabled = true;
                    }
                    else
                    {
                        hlPrev.NavigateUrl = "?page=" + (curPage - 1);
                    }
                    if (curPage >= pageCount)//若当前页为最后一页,则下一页和尾页的链接不可用
                    {
                        hlFirst.Enabled = true;
                        hlPrev.Enabled = true;
                        hlNext.Enabled = false;
                        hlLast.Enabled = false;
                    }
                    else
                    {
                        hlNext.NavigateUrl = "?page=" + (curPage + 1);
                    }
                    hlFirst.NavigateUrl = "?page=1";
                    hlLast.NavigateUrl = "?page=" + pageCount;
                }
                lblPageCount.Text = pageCount.ToString();
                lblCurrentPage.Text = curPage.ToString();
            }
        }
        else//若数据源中无信息,则显示相关提示
        {
            rpTest.Visible = false;
            lblMessage.Text = "无相关记录";
        }
    }

 

//显示页码导航
    protected void rpTest_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
        if (GetPds().DataSourceCount != 0)
        {
            if (e.Item.ItemType == ListItemType.Footer)
            {
                PlaceHolder ph = (PlaceHolder)e.Item.FindControl("numeric");
                int pageCount = GetPds().PageCount;
                int curPage = GetPds().CurrentPageIndex + 1;
                int startIndex = (curPage - 1) / 10 * 10 + 1;//根据当前页得到第一个页码
                int endIndex = startIndex + 9;//显示10个页码
                if (endIndex > pageCount)//判断是否为最后一页
                {
                    endIndex = pageCount;
                }
                for (int i = startIndex; i <= endIndex; i++)//通过循环动态显示页码
                {
                    var hl = new HyperLink { ID = "hl" + i, Text = "[" + i + "]", NavigateUrl = "?page=" + i };
                    ph.Controls.Add(i != curPage ? (Control)hl : new LiteralControl(i.ToString()));
                    ph.Controls.Add(new LiteralControl(" "));
                }
            }
        }
    } 

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统