幸运星空

Lucker的程序人生

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

DataList可以一次显示多条记录且风格自由,可以制作出十分优秀的界面,但其本身不具有分页功能.不过我们可以通过增加几个Label控件,让DataList实现分页功能,示例代码如下:

<tr>
                            <td>
                                <asp:Label ID="lblCurPage" runat="server" Text="Label"></asp:Label>
                            </td>
                            <td>
                                <asp:HyperLink ID="lnkFirst" runat="server">第一页</asp:HyperLink>
                                <asp:HyperLink ID="lnkPerv" runat="server">上一页</asp:HyperLink>
                                <asp:HyperLink ID="lnlNext" runat="server">下一页</asp:HyperLink>
                                <asp:HyperLink ID="lnkLast" runat="server">最后一页</asp:HyperLink>
                            </td>
</tr>

protected void DataListBind()
       {
           AccessDataSource ads = new AccessDataSource();
           ads.SelectCommand = "select * from board order by board_time desc";
           ads.DataFile = "~/App_Data/news.mdb";
           DataView dv = (DataView)ads.Select(DataSourceSelectArguments.Empty);
           PagedDataSource objPage = new PagedDataSource();
           objPage.DataSource = dv;
           objPage.AllowPaging = true;
           objPage.PageSize = 2;
           Int32 CurPage,TolPage;
           TolPage = objPage.PageCount;
           if (Request.QueryString["Page"] != null)
           {
               CurPage = Convert.ToInt32(Request.QueryString["Page"]);
           }
           else
           {
               CurPage = 1 ;
           }
           objPage.CurrentPageIndex = CurPage - 1;
           lblCurPage.Text = "当前第" + CurPage.ToString() + "页,共" + TolPage.ToString()+"页";

           lnkFirst.NavigateUrl=Request.CurrentExecutionFilePath+"?Page=1&main_id="+Request.QueryString["main_id"];
           lnkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + TolPage.ToString() + "&main_id=" + Request.QueryString["main_id"];
           if (!objPage.IsFirstPage)
           {
               lnkPerv.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1) + "&main_id=" + Request.QueryString["main_id"];
           }
           else
           {
               lnkPerv.Visible = false;
               lnkFirst.Visible = false;
           }
           if (!objPage.IsLastPage)
           {
               lnlNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1) + "&main_id=" + Request.QueryString["main_id"];
           }
           else
           {
               lnlNext.Visible = false;
               lnkLast.Visible = false;
           }
           DataList1.DataSource = objPage;
           DataList1.DataBind();
       }

posted on 2008-11-22 21:01  Lucker  阅读(234)  评论(0编辑  收藏  举报