我用使用PagedDataSource类实现DataList控件的分页显示,程序是这样写的:
           '设置分页显示的属性
            If Not (Request.QueryString("Page") Is Nothing) Then
                CurPage = Convert.ToInt32(Request.QueryString("Page"))
            Else
                CurPage = 1
            End If
            objPds.CurrentPageIndex = CurPage - 1
            lblCurrentPage.Text = "Page: " + CurPage.ToString
            '设置分页显示的前一页 和 后一页
            If Not objPds.IsFirstPage Then
                lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1)
            End If
            
            If Not objPds.IsLastPage Then
                lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1)
            End If
            '绑定数据显示
            DataList1.DataSource = objPds
            DataList1.DataBind()问题:
现在我想在 “前一页(lnkPrev)” “后一页(lnkNext)”之间加入比如这样的链接前一页 [1][2][3]  后一页,就是动态计算出所有条目总共占几页,然后动态地加入[1][2][3]页的链接,
这个该如何实现呢?请各位前辈指点,谢谢!

解决方案 »

  1.   

    怎么这样麻烦的
    用AspNetPager吧
    最简单的分页控件
    www.webdiyer.com
      

  2.   

    补充:
         忘了,最前面还有一段程序
                '对PagedDataSource 对象的相关属性赋值
                Dim objPds As PagedDataSource = New PagedDataSource()
                objPds.DataSource = ds.Tables(0).DefaultView
                objPds.AllowPaging = True
                objPds.PageSize = 5
                Dim CurPage As Integer
    总数/每页显示数,这个做了以后应该怎样动态地添加[1][2][3]页的链接呢?
      

  3.   

    得到 总数/每页显示数  后根据mod计算的余数判断总页数需不需要加1,然后用循环写出带链接的数字即可
      

  4.   

    "用循环写出带链接的数字即可"
    这个“数字"需要怎么弄呢?需要用HyperLink控件吗?能给各详细的例子吗?谢谢前辈,或者把"用循环写出带链接的数字即可"的部分的代码简单写给我看看行吗?谢谢前辈!
      

  5.   

    用循环了,先得出多少页,for(int i=0;i<pagecount;i++)
    <a href="aa.asp?id=i>'"+i+"'</a>
      

  6.   

    用循环了,先得出页数,for(int i=0i<pagecount;i++)
    <a href="aa.asp?id=i>'"+i+"'</a>
      

  7.   

    private void ShowStats()
    {
    lblCurrentIndex.Text = "第 " + (dgdFolder.CurrentPageIndex + 1).ToString() + " 页";
    lblPageCount.Text = "总共 " + dgdFolder.PageCount.ToString() + " 页";
    } public void PagerButtonClick(object sender, EventArgs e)
    {
    string arg = ((LinkButton)sender).CommandArgument.ToString();
    switch(arg)
    {
    case "next":
    if (dgdFolder.CurrentPageIndex < (dgdFolder.PageCount - 1))
    {
    dgdFolder.CurrentPageIndex += 1;
    }
    break;
    case "prev":
    if (dgdFolder.CurrentPageIndex > 0)
    {
    dgdFolder.CurrentPageIndex -= 1;
    }
    break;
    case "last":
    dgdFolder.CurrentPageIndex = (dgdFolder.PageCount - 1);
    break;
    default:
    dgdFolder.CurrentPageIndex = System.Convert.ToInt32(arg);
    break;
    }
    BuildConstItems(folderid);
    dv1=ds.Tables[0].DefaultView;
    BindGrid(dv1);
    ShowStats();
    }
    public void dgdFolder_Page(object sender, DataGridPageChangedEventArgs e)
    {
    int startIndex ;
    startIndex = dgdFolder.CurrentPageIndex * dgdFolder.PageSize;
    dgdFolder.CurrentPageIndex = e.NewPageIndex;
    ShowStats();
    }
      

  8.   

    可以看看ASPNETPAGERS吴旗娃的分页控件。
      

  9.   

    我现在思路到是有了,就是最后一步,该如何“动态地产生[1][2][3]这些链接呢”?
    是动态的生成HyperLink控件对象嘛?那该如何加在页面上呢?谢谢前辈们指点!谢谢!
      

  10.   

    我现在思路到是有了,就是最后一步,该如何“动态地产生[1][2][3]这些链接呢”?
    是动态的生成HyperLink控件对象嘛?那该如何加在页面上呢?谢谢前辈们指点!谢谢!
      

  11.   

    我自己写的分页用户控件,给你参考一下
    html代码:
    <%@ Control Language="c#" AutoEventWireup="false" Codebehind="WebTurnPage.ascx.cs" Inherits="Chsoft.Fyyy.WebTurnPage" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
    <asp:label id="lb_page" runat="server" DESIGNTIMEDRAGDROP="61"></asp:label>
    <asp:LinkButton id="lb_firstPage" runat="server" Enabled="False">首页</asp:LinkButton>
    <asp:LinkButton id="lb_previousPage" runat="server" Enabled="False">上页</asp:LinkButton>
    <asp:linkbutton id="lb_nextPage" runat="server" Enabled="False">下页</asp:linkbutton>
    <asp:LinkButton id="lb_lastPage" runat="server" Enabled="False">末页</asp:LinkButton>&nbsp;&nbsp;跳转到
    <asp:DropDownList id="ddl_turnPage" runat="server" AutoPostBack="True" Enabled="False"></asp:DropDownList>页
    cs代码:namespace Chsoft.Fyyy
    {
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls; /// <summary>
    /// 显示分页链接的控件
    /// 编写:罗将神
    /// 时间 2005-7-27
    /// </summary>
    public class WebTurnPage : System.Web.UI.UserControl
    { public int currentPage=1 ;
    public int totalPage=1;

    private int  _pageSize=20;
    public int   pageSize
    {
    get 
    {
    return  _pageSize;
    }
    set
    {
    _pageSize=value;
    }
    }

    private int  _recordCount;
    public int   recordCount
    {
    get 
    {
    return  _recordCount;
    }
    set
    {
    _recordCount=value; }
    }

    protected System.Web.UI.WebControls.LinkButton lb_firstPage;
    protected System.Web.UI.WebControls.LinkButton lb_previousPage;
    protected System.Web.UI.WebControls.LinkButton lb_nextPage;
    protected System.Web.UI.WebControls.LinkButton lb_lastPage;
    protected System.Web.UI.WebControls.DropDownList ddl_turnPage;
    protected System.Web.UI.WebControls.Label lb_page;

    private void Page_Load(object sender, System.EventArgs e)
    {

    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);

    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器
    /// 修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.lb_firstPage.Click += new System.EventHandler(this.lb_firstPage_Click);
    this.lb_previousPage.Click += new System.EventHandler(this.lb_previousPage_Click);
    this.lb_nextPage.Click += new System.EventHandler(this.lb_nextPage_Click);
    this.lb_lastPage.Click += new System.EventHandler(this.lb_lastPage_Click);
    this.ddl_turnPage.SelectedIndexChanged += new System.EventHandler(this.ddl_turnPage_SelectedIndexChanged);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion

    /// <summary>
    /// 生成获取当前页数据的sql语句
    /// </summary>
    /// <param name="sql"></param>
    /// <param name="orderID"></param>
    /// <returns></returns> public string  GetCurrentPageSql(string sql,string orderID)
    {
    int currentPageSize=this.pageSize; 
    if(this.currentPage>=this.totalPage  ) //最后一页的记录数量
    currentPageSize = this.recordCount-(this.pageSize*(this.totalPage-1));

    string pageSql =" ";
    string  Sql1 = sql.Insert(6,"   top "+ this.currentPage*this.pageSize + "  ");
    pageSql += "select top {0} * from ( Select top {0}  *  from ({1} order by {2} desc) DERIVEDTBL order by  {2} asc) DERIVEDTBL order by {2} desc ";
    pageSql = string.Format(pageSql,currentPageSize,Sql1,orderID);
    return pageSql;
    }

    /// <summary>
    /// 获取总记录数并显示翻页链接
    /// </summary>
    /// <param name="m_recordCount"></param>
    /// <param name="bindType"></param> public void GetRecordCount(int m_recordCount,bool bindType)
    {
    this.recordCount = m_recordCount;
    totalPage =( recordCount / pageSize);                        //计算总页数
    if (recordCount % pageSize > 0)
    totalPage++;


    if(bindType==false)
    {
    ViewState["currentPage"]=1;
    ViewState["totalPage"]=this.totalPage;
    }
    currentPage = (int)ViewState["currentPage"];

    ShowPage(); } private void ShowPage()
    {
    string  strTemp="";
    if ( pageSize>0 &&  recordCount > pageSize && currentPage <= totalPage && totalPage>1) //满足分页的条件则显示分页链接
    {        
    strTemp = strTemp + " 总共有 " + this.recordCount + "条记录   ";  
    strTemp = strTemp + " 当前第 " + currentPage + "/" + totalPage + " 页 ";              
         
    if(totalPage>1)
    {
    ddl_turnPage.Enabled = true;
    int[]  iPageCount = new int[totalPage];
    for(int i=1;i<=totalPage;i++)
    iPageCount[i-1]=i;

    this.ddl_turnPage.DataSource = iPageCount;
    ddl_turnPage.DataBind();
    ddl_turnPage.SelectedIndex = this.currentPage-1;    
    }

    this.lb_page.Text= strTemp;

    this.lb_firstPage.Enabled = true;
    this.lb_previousPage.Enabled =true;
    this.lb_nextPage.Enabled = true;
    this.lb_lastPage.Enabled = true;

    if(this.currentPage<2 )
    {
    this.lb_firstPage.Enabled = false;
    this.lb_previousPage.Enabled =false; this.lb_nextPage.Enabled = true;
    this.lb_lastPage.Enabled =true;
    }
    if(this.currentPage>=this.totalPage)
    {
    this.lb_firstPage.Enabled = true;
    this.lb_previousPage.Enabled =true;
    this.lb_nextPage.Enabled = false;
    this.lb_lastPage.Enabled = false;
    }

    } else //显示无数据状态
    {
    this.lb_page.Text= "";
    this.lb_firstPage.Enabled = false;
    this.lb_previousPage.Enabled =false;
    this.lb_nextPage.Enabled = false;
    this.lb_lastPage.Enabled = false;



    this.ddl_turnPage.Items.Clear();
    this.ddl_turnPage.Enabled = false; } } /// <summary>
    /// 定义翻页事件
    /// </summary>
    public event EventHandler pageChange;
    protected virtual void   OnpageChange(EventArgs e)
    {
    if(pageChange!=null)
    pageChange(this,e);
    }
    /// <summary>
    /// 处理点击翻页按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void lb_firstPage_Click(object sender, System.EventArgs e)
    {
    ViewState["currentPage"] = 1;
    this.currentPage = (int)ViewState["currentPage"];
    OnpageChange(e);  
    }

    private void lb_previousPage_Click(object sender, System.EventArgs e)
    {
    ViewState["currentPage"] = (int)ViewState["currentPage"]-1;
    if((int)ViewState["currentPage"]<1)
    ViewState["currentPage"]=1;
    this.currentPage = (int)ViewState["currentPage"];
    OnpageChange(e);  
    } public void lb_nextPage_Click(object sender, System.EventArgs e)
    {
    ViewState["currentPage"] = (int)ViewState["currentPage"]+1;
    if(this.currentPage>=(int)ViewState["totalPage"])
    this.currentPage = (int)ViewState["totalPage"];
    else
    this.currentPage = (int)ViewState["currentPage"];

    OnpageChange(e);  
    }


    private void lb_lastPage_Click(object sender, System.EventArgs e)
    {
    ViewState["currentPage"] = (int)ViewState["totalPage"];
    this.currentPage = (int)ViewState["currentPage"];

    OnpageChange(e);  
    }
    private void ddl_turnPage_SelectedIndexChanged(object sender, System.EventArgs e)
    {

    ViewState["currentPage"] =  ddl_turnPage.SelectedIndex+1;
    this.currentPage = (int)ViewState["currentPage"];

    OnpageChange(e);  
    }


    }
    }