首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

DataList中的分页中首页、下一页、上一页、末页功能 如何写

2012-06-15 
DataList中的分页中首页、下一页、上一页、末页功能 怎么写?DataList中的分页中首页、下一页、上一页、末页功能

DataList中的分页中首页、下一页、上一页、末页功能 怎么写?
DataList中的分页中首页、下一页、上一页、末页功能 怎么写? DataList中的分页中首页、下一页、上一页、末页功能 怎么写?

[解决办法]
这个是我写的datalist分页代码,已经分装成控件.需要传的参数已经注释说明

C# code
using System; 
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace B2B.UC
{
  public partial class UCPaged : System.Web.UI.UserControl
  {
    #region 属性组
    /// <summary>
    /// 源数据源
    /// </summary>
    private ArrayList objAL;
    public ArrayList ObjAL
    {
      get { return objAL; }
      set { objAL = value; }
    }

    /// <summary>
    /// 源数据控件
    /// </summary>
    private DataList objDl;
    public DataList ObjDl
    {
      get { return objDl; }
      set { objDl = value; }
    }

    /// <summary>
    /// 源页面
    /// </summary>
    private string pageName;
    public string PageName
    {
      get { return pageName; }
      set { pageName = value; }
    }

    /// <summary>
    /// url参数哈希表
    /// </summary>
    private Hashtable parmHt;
    public Hashtable ParmHt
    {
      get { return parmHt; }
      set { parmHt = value; }
    }

    /// <summary>
    /// 每页显示数
    /// </summary>
    private int pageSize;
    public int PageSize
    {
      get
      {
        if (pageSize == null && pageSize <= 0)
        {
          return 10;
        }
        else
        {
          return pageSize;
        }
      }
      set { pageSize = value; }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
      bindPagedData(objAL, objDl);
    }

    /// <summary>
    /// 获得分页url参数和源页面
    /// </summary>
    /// <param name="sourceHT"> </param>
    /// <returns> </returns>
    private StringBuilder getParmHT(Hashtable sourceHT)
    {
      StringBuilder parmStr = new StringBuilder();
      parmStr = parmStr.Append(pageName);
      parmStr = parmStr.Append("?");
      if (sourceHT != null && sourceHT.Count > 0)
      {
        foreach (DictionaryEntry de in sourceHT)
        {


          parmStr = parmStr.Append(de.Key);
          parmStr = parmStr.Append("=");
          parmStr = parmStr.Append(de.Value);
          parmStr = parmStr.Append("&");
        }

      }
      return parmStr;
    }

    /// <summary>
    /// 绑定分页数据
    /// </summary>
    private void bindPagedData(ArrayList objAL, DataList sourceDL)
    {

      if (objAL != null && objAL.Count != 0)
      {
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = objAL;
        pds.AllowPaging = true;
        pds.PageSize = pageSize;
        int icplIndex = 1;
        if (!String.IsNullOrEmpty(Request.QueryString["page"]))
        {
          icplIndex = int.Parse(Request.QueryString["page"]);
        }
        if (icplIndex < 1)
        {
          icplIndex = 1;
        }
        if (icplIndex > pds.PageCount)
        {
          icplIndex = pds.PageCount;
        }
        ltrl_page.Text = WritePage(pds).ToString();
        pds.CurrentPageIndex = icplIndex - 1;
        sourceDL.DataSource = pds;
        sourceDL.DataBind();
      }
    }
    /// <summary>
    /// 写出页码
    /// </summary>
    private StringBuilder WritePage(PagedDataSource pds)
    {
      StringBuilder page = new StringBuilder();
      int pageID = 1;
      int StartpageID = 1;
      int TenpageID = 1;

      if (!String.IsNullOrEmpty(Request["page"])) Int32.TryParse(Request["page"], out pageID);
      if (!String.IsNullOrEmpty(Request["Startpage"])) Int32.TryParse(Request["Startpage"], out StartpageID);
      if (!String.IsNullOrEmpty(Request["tenpage"])) Int32.TryParse(Request["tenpage"], out TenpageID);

      if (pageID < 1) pageID = 1;
      if (pageID > pds.PageCount) pageID = pds.PageCount;

      StringBuilder parStr = getParmHT(parmHt);
      if (!parStr.Equals(""))
      {
        page = page.Append(" <div class='pagesL'>");
        page = page.Append(" <a href='" + parStr + "page=1&tenpage=1'>第一页 </a> | ");
        page = page.Append(" <a href='" + parStr + "page=" + pds.PageCount.ToString() + "&tenpage=" + Convert.ToString(Math.Floor(Convert.ToDouble(pds.PageCount / 10)) + 1) + "'>最后一页 </a>");
        page = page.Append(" </div>");
        page = page.Append(" <div class='pages'>");
        //按照十页一分来处理
        if (pageID > 10)


        {
          page = page.Append(" <a href='" + parStr + "page=" + Convert.ToString(TenpageID - 10) + "&tenpage=" + Convert.ToString(TenpageID - 1) + "' >上十页 </a>");
        }
        if (pageID <= TenpageID * 10)
        {
          //范围内
          for (int i = (TenpageID - 1) * 10 + 1; i <= (TenpageID * 10 > pds.PageCount ? pds.PageCount : 10); i++)
          {
            if (i == pageID)
            {
              page = page.Append(" <span>" + Convert.ToString(i) + " </span>");
            }
            else
            {
              page = page.Append(" <a href='" + parStr + "page=" + Convert.ToString(i) + "&tenpage=" + Convert.ToString(TenpageID) + "'>" + Convert.ToString(i) + " </a>");
            }
          }
        }
        if (TenpageID * 10 < pds.PageCount)
        {
          page = page.Append(" <a href='" + parStr + "page=" + Convert.ToString(TenpageID + 10) + "&tenpage=" + Convert.ToString(TenpageID + 1) + "' >下十页 </a>");
        }
        page = page.Append(" </div> <div class='clear'> </div>");
      }
      return page;
    }

  }
}


[解决办法]
//前台
<table id="page" width="100%">
<tr align="center">
<td>
<asp:Label ID="lblCurPage" runat="server"></asp:Label>
&nbsp;&nbsp;
<asp:Label ID="lblEachPage" runat="server"></asp:Label>
<asp:Label ID="lblTnum" runat="server"></asp:Label>
&nbsp;&nbsp;
<asp:HyperLink ID="lnkFirst" runat="server">&nbsp;第一页&nbsp;</asp:HyperLink>
<asp:HyperLink ID="lnkPrev" runat="server">&nbsp;上一页&nbsp;</asp:HyperLink>
<asp:HyperLink ID="lnkNext" runat="server">&nbsp;下一页&nbsp;</asp:HyperLink>
<asp:HyperLink ID="lnkLast" runat="server">&nbsp;最后页&nbsp;&nbsp;&nbsp;</asp:HyperLink>
到:<asp:TextBox ID="txtPage" runat="server" Width="32px"></asp:TextBox>
页<asp:Button ID="btnPage" runat="server" OnClick="btnPage_Click" Text="Go" Width="32px" /></td>
</tr>
</table>


//cs文件
/// <summary>
/// News 的摘要说明。
/// 分页。
/// </summary>
public static int TotalPage;//定义变量来保存总页数
public int CurPage;//定义变量来保存当前页索引
public int Tnum;//总条数;
public int EachPage;//每页总条数

protected void Page_Load(object sender, EventArgs e)


{
if (!Page.IsPostBack)
{
this.Newfillgv();
}
}



public void Newfillgv()
{
SqlConnection scon = new SqlConnection(EP.GetConStr()); //调用Web.config连接数据库
scon.Open();//打开数据库连接
string SqlStr = "select * from News order by time"; //数据库连接字符串
SqlDataAdapter da = new SqlDataAdapter(SqlStr, scon);
DataSet ds = new DataSet();
da.Fill(ds, "Table");
PagedDataSource objPage = new PagedDataSource();//创建分页类
objPage.DataSource = ds.Tables["Table"].DefaultView;//设置数据源
objPage.AllowPaging = true;
objPage.PageSize = 8;

if (Request.QueryString["Page"] != null)
{
CurPage = Convert.ToInt32(Request.QueryString["Page"]);
CurPage = Math.Min(CurPage, objPage.PageCount);
CurPage = Math.Max(CurPage, 1);
}
else

CurPage = 1;
objPage.CurrentPageIndex = CurPage - 1;
TotalPage = objPage.PageCount;
Tnum = objPage.DataSourceCount;
EachPage = objPage.Count;
lblCurPage.Text = "第 " + CurPage.ToString() + " / " + TotalPage.ToString() + " 页";
lblTnum.Text = "共: " + Tnum + " 条记录";
lblEachPage.Text = "每页有: " + EachPage.ToString() + " 条记录";

if (objPage.CurrentPageIndex != 0)
lnkFirst.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1);
if (objPage.CurrentPageIndex != TotalPage - 1)
lnkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(TotalPage);

if (!objPage.IsFirstPage)
lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
if (!objPage.IsLastPage)
lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);

DlNew.DataSource = objPage;
DlNew.DataBind();

}


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

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{

}
#endregion

protected void btnPage_Click(object sender, System.EventArgs e)
{
int PageNum = 0;
if (!Request.Form["txtPage"].Equals(""))
PageNum = Convert.ToInt32(Request.Form["txtPage"]);
if (PageNum <= 0 || PageNum > TotalPage)
Response.Redirect(Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1));
else
Response.Redirect(Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(PageNum));
}
自己修改下连接数据库的代码....

热点排行