let it be
行到水穷处 坐看云起时

  pagertemplate是GridView控件的一个模板,主要用来实现分页的功能。在GridView控件没有使用手工设置进行和数据源连接的情况下,也就是控件连控件的设置,可以使用这个模板来实现自动分页的功能。
  不多说了,具体给出实现方法:
1)在GridView控件的pagertemplate模板中进行如下设置:
                        <PagerTemplate>
                        第<asp:Label id="lblPageIndex" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1  %>' />页
                        共/<asp:Label id="lblPageCount" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageCount  %>' />页
                        <asp:linkbutton id="btnFirst" runat="server" causesvalidation="False" commandargument="First" commandname="Page" text="首页" />
                        <asp:linkbutton id="btnPrev" runat="server" causesvalidation="False" commandargument="Prev" commandname="Page" text="上一页" />
                        <asp:linkbutton id="btnNext" runat="server" causesvalidation="False" commandargument="Next" commandname="Page" text="下一页" />                         
                        <asp:linkbutton id="btnLast" runat="server" causesvalidation="False" commandargument="Last" commandname="Page" text="尾页" />                                           
                        </PagerTemplate>
2)在对应的后台代码中生成GridView控件的PageIndexChanging()事件,具体代码如下:
    protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //创建一个 GridView
        GridView theGrid = sender as GridView; 
        int newPageIndex = 0;
        if (-2 == e.NewPageIndex)
        {
            // 单击下一页
            TextBox txtNewPageIndex = null;
            GridViewRow pagerRow = theGrid.BottomPagerRow;

            if (null != txtNewPageIndex)
            {
                newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
            }
        }
        else
        {
            newPageIndex = e.NewPageIndex;
        }
        newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
        newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
        theGrid.PageIndex = newPageIndex;
    }

以上代码仅供参考,如果谁有更好的自动分页的方法可以贴出来交流!
提前祝大家圣诞节快乐!

posted on 2007-12-24 08:58  流浪浪  阅读(4089)  评论(16编辑  收藏  举报