Gridview自定义分页(转载)

 girdview 分页PageIndexChanging事件问题昨天弄了一下午,总是提示激发了未处理的事件“PageIndexChanging
修改方法:一要在girdview 后面加 OnPageIndexChanging="GridView1_PageIndexChanging
                二要cs页面中添加
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        DataBind();
    }
OK问题解决!!
  下面的是一个比较齐全的分页代码,
protected void Page_Load(object sender, EventArgs e)
    {
        btnFirst.Text = "最首页";
        btnPrev.Text = "前一页";
        btnNext.Text = "下一页";
        btnLast.Text = "最后页";
        GridView1.DataBind();
        ShowPageChangedStatus();
    }
   private void ShowPageChangedStatus()
    {
        lblCurrentIndex.Text = "第" + (GridView1.PageIndex + 1).ToString() + "页";
        lblPageCount.Text = "总共 " + GridView1.PageCount.ToString() + " 页";
    }
    public void PagerButtonClick(object sender, EventArgs e)
    {
        string arg = ((LinkButton)sender).CommandArgument.ToString();
        switch (arg)
        {
            case "next":
                if (GridView1.PageIndex < (GridView1.PageCount - 1))
                {
                    GridView1.PageIndex += 1;
                }
                break;
            case "prev":
                if (GridView1.PageIndex > 0)
                {
                    GridView1.PageIndex -= 1;
                }
                break;
            case "last":
                GridView1.PageIndex = (GridView1.PageCount - 1);
                break;
            default:
                GridView1.PageIndex = System.Convert.ToInt32(arg);
                break;
        }
        GridView1.DataBind();
        ShowPageChangedStatus();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        int startIndex;
        startIndex = GridView1.PageIndex * GridView1.PageSize;
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
        ShowPageChangedStatus();
    }

  <asp:label id="lblPageCount" runat="server"></asp:label>&nbsp;
  <asp:label id="lblCurrentIndex" runat="server"></asp:label>
  <asp:linkbutton id="btnFirst" onclick="PagerButtonClick" runat="server"
   Font-size="8pt" ForeColor="navy" CommandArgument="0"></asp:linkbutton>&nbsp;
  <asp:linkbutton id="btnPrev" onclick="PagerButtonClick" runat="server"
   Font-size="8pt" ForeColor="navy" CommandArgument="prev"></asp:linkbutton>&nbsp;
  <asp:linkbutton id="btnNext" onclick="PagerButtonClick" runat="server"
   Font-size="8pt" ForeColor="navy" CommandArgument="next"></asp:linkbutton>&nbsp;
  <asp:linkbutton id="btnLast" onclick="PagerButtonClick" runat="server"
   Font-size="8pt" ForeColor="navy" CommandArgument="last"></asp:linkbutton>

 

下面是转载自博客园——人字拖

前些天我写了关于 <<在存储过程中实现分页>>和<<GridView控件事件详解 >> ,后来又有一些人问我怎样在GridView中应用这个东东!其实很简单,主要是怎么保存当前页面的页码PageIndex问题,不过把这个解决了什么都好办了.因为在分页过程中:PageSize是一定的,我们可以用一个属性来表示.保存PageIndex好多中方法,而且数据不是很庞大,基本不会好太多的资源.还是一句老话,话再多都没有例子直观.

  在这里我们将用一个隐藏字段来保存这个PageIndex,即当前页码.当点击上一页时,将它的值减一,知道为0,要注意的一点这里的第一页页码是0而不是1.下面看看代码,然后我们再分析分析!

 1<asp:GridView ID="NewsGrid" runat="server" AutoGenerateColumns="False" AllowPaging="false" Width="100%">
 2            <Columns>
 3                <asp:BoundField DataField="NewsId" HeaderText="新闻ID"/>
 4                <asp:HyperLinkField DataNavigateUrlFields="NewsId" DataNavigateUrlFormatString="~/Details.aspx?ID={0}"
 5                    DataTextField="Title" HeaderText="新闻标题"  ItemStyle-Width="70%"/>
 6                <asp:BoundField DataField="PostTime" HeaderText="发布时间"/>
 7                <asp:CommandField HeaderText="新闻管理" ShowCancelButton="False" ShowDeleteButton="True"
 8                    ShowEditButton="True"/>
 9            </Columns>
10        </asp:GridView>
11        <div style=" height:16px; padding-top:5px; margin-right:30px; float:right">
12        <asp:HiddenField ID="CurrentPage" runat="server" Value="0"/>
13        <asp:LinkButton ID="First" runat="server" CommandArgument="first" OnClick="PagerButton_Click">首 页</asp:LinkButton>
14        <asp:LinkButton ID="Prev" runat="server" CommandArgument="prev" OnClick="PagerButton_Click">上一页</asp:LinkButton>
15        <asp:LinkButton ID="Next" runat="server" CommandArgument="next" OnClick="PagerButton_Click">下一页</asp:LinkButton>
16        <asp:LinkButton ID="Last" runat="server" CommandArgument="last" OnClick="PagerButton_Click">尾 页</asp:LinkButton>
17        </div>

  CS文件中的代码:

 1        protected void PagerButton_Click(object sender, EventArgs e)
 2        {
 3            int pageIndx = Convert.ToInt32(CurrentPage.Value);
 4            int totals = NewsManager.GetNews(0, pageSize).TotalRecords;
 5            int pages = (totals % pageSize) == 0 ? (totals / pageSize) : (totals / pageSize + 1);
 6            string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();
 7            switch (arg)
 8            {
 9                case "prev":
10                    if (pageIndx > 0)
11                    {
12                        pageIndx -= 1;
13                    }

14                    break;
15                case "next":
16                    if (pageIndx < pages - 1)
17                    {
18                        pageIndx += 1;
19                    }

20                    break;
21                case "last":
22                    pageIndx = pages - 1;
23                    break;
24                default:
25                    pageIndx = 0;
26                    break;
27            }

28            CurrentPage.Value = pageIndx.ToString();
29            NewsGrid.DataSource = NewsManager.GetNews(pageIndx , pageSize).Entities;
30            NewsGrid.DataBind();
31        }
posted @ 2009-05-04 15:46  智者生存  阅读(200)  评论(0编辑  收藏  举报