AspNetPager.dll 实现分页

 

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.Data.SqlClient;
namespace fenye
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindGrid();
        }
    }

    private SqlConnection con;
    //private string DBName = "tongjinet";

    //创建连接对象并打开
    public void Open()
    {
        if (con == null)
          //con = new SqlConnection("server=(local);uid=sa;pwd=sql;database=" + DBName);
    con = new SqlConnection(ConfigurationSettings.AppSettings["con"]);
        if (con.State == ConnectionState.Closed)
            con.Open();
    }

    //创建一个命令对象并返回该对象
    public SqlCommand CreateCommand(string sqlStr)
    {
        Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = sqlStr;
        cmd.Connection = con;
        return cmd;
    }

    //生成一个对象并返回该结果集第一行第一列
    public object GetScalar(string sqlStr)
    {
        SqlCommand cmd = CreateCommand(sqlStr);
        object obj = cmd.ExecuteScalar();
        //CommadnBehavior.CloseConnection是将于DataReader的数据库链接关联起来 
        //当关闭DataReader对象时候也自动关闭链接
        return obj;
    }

    //执行数据库查询并返回一个数据集 [当前页码,每页记录条数]
    public DataSet GetCurrentPage(int pageIndex, int pageSize)
    {
        //设置导入的起始地址
        int firstPage = pageIndex * pageSize;
        string sqlStr = "select * from news order by id desc";
        SqlCommand cmd = CreateCommand(sqlStr);
        DataSet dataset = new DataSet();
        SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
        dataAdapter.Fill(dataset, firstPage, pageSize, "news");
        cmd.Dispose();
        Close();
        dataAdapter.Dispose();
        return dataset;
    }
    //获得查询数据的总条数
    public object GetAllCount()
    {
        string sqlStr = "select count(*) from news";
        object obj = GetScalar(sqlStr);
        return obj;
    }
    //关闭数据库
    public void Close()
    {
        if (con != null)
        {
            con.Close();
        }
    }
    //释放资源
    public void Dispose()
    {
        if (con != null)
        {
            con.Dispose();
            con = null;
        }
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindGrid();
    }

    public void BindGrid()
    {
        this.AspNetPager1.RecordCount = Int32.Parse(GetAllCount().ToString());
        int pageIndex = this.AspNetPager1.CurrentPageIndex - 1;
        int pageSize = this.AspNetPager1.PageSize = 5;
        Repeater1.DataSource = GetCurrentPage(pageIndex, pageSize);
        Repeater1.DataBind();
    }


    }
}

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="fenye.WebForm2" %>
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>用AspNetPager.dll控件的分页方法操作方法</title>
    <style type="text/css">
    body{font-size: 12px;}
    ul{padding:0;margin:0;list-style-position:inside;}
    li{height:24px;line-height:24px;border-bottom:1px solid dotted;list-style-type:none;list-style-image:url(image/li1.gif);width:290px;}
    .show1{width:320px;border:1px solid #92B0DD;background-color:#FFFFFF;overflow:hidden;padding:0 0 10px 0;}
    .show1 h5{margin:1px;background-color:#E2EAF8;height:24px;line-height:24px;padding:0 0 0 10px;}
    a{text-decoration:none;color:Black;}
    a:visited{text-decoration:none;color:Black;}
    a:hover{text-decoration:none;color:Red;}
    
    .mycode{ width:350px; height:130px; float:left;}    
    .mycode ul{ width:300px;}    
    .mycode li{ width:150px; float:left; display:block;}    
body {
background-image: url(images/bg.jpg);
}

</style>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div class="mycode">
       <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
        <ul>
        <li><%#DataBinder.Eval(Container.DataItem, "id")%></li>
        <li><%#DataBinder.Eval(Container.DataItem, "title")%></li>
        </ul>
        </ItemTemplate>
        </asp:Repeater>
    </div>
        <webdiyer:AspNetPager ID="AspNetPager1" runat="server" Width="100%" NumericButtonCount="6" UrlPaging="true" NumericButtonTextFormatString="[{0}]" CustomInfoHTML="第 <font color='red'><b>%CurrentPageIndex%</b></font> 页 共 %PageCount% 页 显示 %StartRecordIndex%-%EndRecordIndex% 条" ShowCustomInfoSection="left"
FirstPageText="首页" LastPageText="末页" NextPageText="下页" PrevPageText="上页" Font-Names="Arial" BackColor="#F8B500" AlwaysShow="true" ShowInputBox="Always" SubmitButtonText="跳转" SubmitButtonStyle="botton" OnPageChanged="AspNetPager1_PageChanged">
        </webdiyer:AspNetPager>


<%--// NumericButtonCount="6"                 显示页码的个数 
// NumericButtonTextFormatString="[{0}]" 页码显示的格式
// ShowCustomInfoSection="left"           显示左边的(第几页、共几页、显示几条)
// TextBeforeInputBox="转到第"            InputBox前面显示的字
// TextAfterInputBox="页"                 InputBox后面显示的字
// PagingButtonSpacing="10px"             字与字间的距离--%>
    </div>

 

    </form>
</body>
</html>

 

posted @ 2011-05-31 13:03  likunran  阅读(129)  评论(0编辑  收藏  举报