今天用三层架构做的新闻的分页,用的是第三方控件<AspNetPager>下面就介绍一下如何使用第三方控件AspNetPager

第一步:先在网上下载AspNetPager.dll文件。

第二步:把AspNetPager.dll文件添加到选项卡中。(打开<工具箱>,在空白处右击鼠标,选择<选择项>,浏览找到<AspNetPager.dll>点击确定),这样就把AspNetPager控件引进来了。

第三步:把AspNetPager文件引入到bin文件夹下。

第四步:把AspNetPager控件拖进你要用分页的页面。(准备工作完成,写代码)

DBtitly 层:

  引用SqlHelper

DAL层:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DButility;
using Model;
using System.Data;
using BibleDAL.SqlHelper;
namespace DAL
{
public class News
{
/// <summary>
/// 新闻分页列表
/// </summary>
/// <param name="page">当前页</param>
/// <param name="pageSize">每页显示多少条</param>
/// <returns>分页列表</returns>
public List<NewsInfo> GetNewsByPage(int page, int pageSize)
{
List
<NewsInfo> newsList = new List<NewsInfo>();
SqlConnection conn
= GetConn();
string sql = "select top " + pageSize + " * from news where id not in (select top " + (page - 1) * pageSize + " id from news order by times desc ) order by times desc";
DataSet ds
= SqlHelper.ExecuteDataset(conn, CommandType.Text, sql);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
NewsInfo info
= new NewsInfo();
info.Id
= Convert.ToInt32(ds.Tables[0].Rows[i]["id"].ToString());
info.Title
= ds.Tables[0].Rows[i]["title"].ToString();
info.Contents
= ds.Tables[0].Rows[i]["contents"].ToString();
info.Times
= Convert.ToDateTime(ds.Tables[0].Rows[i]["times"].ToString());
newsList.Add(info);
}
return newsList;
}

/// <summary>
/// 根据id得到此条新闻
/// </summary>
/// <param name="id">新闻id</param>
/// <returns>新闻内容</returns>
public NewsInfo GetNewsById(int id)
{
SqlConnection conn
= GetConn();
string sql = "select * from news where id=" + id;
SqlCommand comm
= new SqlCommand(sql, conn);
comm.Parameters.AddWithValue(
"@id", id);
DataSet ds
= SqlHelper.ExecuteDataset(comm);

NewsInfo info
= new NewsInfo();

if (ds.Tables[0].Rows.Count>0)
{
info.Id
= Convert.ToInt32(ds.Tables[0].Rows[0]["id"].ToString());
info.Title
= ds.Tables[0].Rows[0]["title"].ToString();
info.Contents
= ds.Tables[0].Rows[0]["contents"].ToString();
info.Times
= Convert.ToDateTime(ds.Tables[0].Rows[0]["times"].ToString());
}
return info;
}
/// <summary>
/// 得到此表中总共有多少条新闻信息
/// </summary>
/// <returns>多少条</returns>
public int GetRecordCount()
{
SqlConnection conn
= GetConn();
string sql = "select count(*) from news";
SqlCommand comm
= new SqlCommand(sql, conn);
int cnt = (int)comm.ExecuteScalar();
conn.Close();
return cnt;
}


/// <summary>
/// 得到数据库连接对象
/// </summary>
/// <returns></returns>
public static SqlConnection GetConn()
{
SqlConnection conn
= new SqlConnection();
conn.ConnectionString
= "server=.;database=zhihuigu;uid=sa;pwd=;";
conn.Open();
return conn;
}
}
}

BLL层:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using DAL;

namespace BLL
{
    public class News
    {
        public DAL.News news = new DAL.News();
        /// <summary>
        /// 根据分页显示新闻
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<NewsInfo> NewsInfoByPage(int page,int pageSize)
        {
            return news.GetNewsByPage(page, pageSize);
        }
        /// <summary>
        /// 得到该id对应的新闻
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public NewsInfo GetNewsById(int id)
        {
            return news.GetNewsById(id);
        }
        /// <summary>
        /// 得到新闻的总条数
        /// </summary>
        /// <returns></returns>
        public int GetRecordCount()
        {
            return news.GetRecordCount();
        }
    }
}

UI层:(用Repeater绑定做的)

aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="newsinfoBypage.aspx.cs" Inherits="newsinfoBypage" %>

<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
    
</head>
<body>

    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
            <ul>
        </HeaderTemplate>
        
        <ItemTemplate>
            <li>
                <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#DataBinder.Eval(Container.DataItem,"id","looknews.aspx?id={0}") %>'><%#Eval("title") %></asp:HyperLink>
            </li>
        </ItemTemplate>
        
        <FooterTemplate>
            </ul>
        </FooterTemplate>
        </asp:Repeater>
        <webdiyer:AspNetPager ID="AspNetPager1" runat="server" OnPageChanged="AspNetPage1_PageChanged">
    </webdiyer:AspNetPager>
    s    </div>
    </form>
</body>
</html>

 .cs

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 BLL;
using Model;




public partial class newsinfoBypage : System.Web.UI.Page
{
    public BLL.News news = new News();
    protected void Page_Load(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 1;//当前显示页默认第一页
        AspNetPager1.PageSize = 2;//每页显示几条信息
        AspNetPager1.RecordCount = Convert.ToInt32(news.GetRecordCount());//总共有多少页?
        Bindnews();
    }
    private void Bindnews()
    {
        Repeater1.DataSource = news.NewsInfoByPage(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize);
        Repeater1.DataBind();
    }
    protected void AspNetPage1_PageChanged(object sender, EventArgs e)
    {
        Bindnews();
    }
}

至此关于AspNetPager的分页就完成了,有什么错误的地方希望大家指出来。

posted on 2011-03-26 19:10  n泛泛之交w  阅读(740)  评论(0编辑  收藏  举报