AspNetPager分页控件使用方法

来源:互联网 发布:数据库系统原理知识点 编辑:程序博客网 时间:2024/05/01 03:54
今天做项目时学会一种新控件AspNetPager.dll用法,很想把这方面的经验和大家分享一下,欢迎大家一起进来讨论讨论!
一、前台显示界面代码Default.aspx(注意,代码运行环境是VS.2005)
<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="Default4"%>
<%@RegisterAssembly="AspNetPager"Namespace="Wuqi.Webdiyer"TagPrefix="webdiyer"%>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headid="Head1"runat="server">
    <title>用AspNetPager.dll控件的分页方法操作方法</title>
</head>
<body>
    <formid="form1"runat="server">
    <div>
    <tableborder=1>
       <asp:RepeaterID="Repeater1"runat="server">
        <ItemTemplate>
        <tr>
        <td><%#DataBinder.Eval(Container.DataItem,"osid")%></td>
        <td><%#DataBinder.Eval(Container.DataItem,"year1")%></td>
        <td><%#DataBinder.Eval(Container.DataItem,"month1")%></td>
        <td><%#DataBinder.Eval(Container.DataItem,"output1")%></td>
        </tr>
        </ItemTemplate>
        </asp:Repeater>
    </table>
<webdiyer:AspNetPagerID="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>
             
<%--<webdiyer:AspNetPager ID="AspNetPager1" runat="server" PageSize="15" style="font-size:14px;" HorizontalAlign="Right" NumericButtonCount="6" NumericButtonTextFormatString="[{0}]" CustomInfoHTML="第 <font color='red'><b>%CurrentPageIndex%</b></font> 页 共 %PageCount% 页 显示 %StartRecordIndex%-%EndRecordIndex% 条" InputBoxStyle="width:24px; height:14px;" ShowInputBox="Always" SubmitButtonText=" GO " FirstPageText="[首 页]" PrevPageText="[上 页]" NextPageText="[下 页]" LastPageText="[末 页]" TextBeforeInputBox="转到第" TextAfterInputBox="页 " PagingButtonSpacing="10px" width="100%" ShowCustomInfoSection="Left" UrlPaging="true"></webdiyer:AspNetPager>
--%>
    </div>
    </form>
</body>
</html>
二、Default.aspx.cs页面的代码
    DBAccess db =newDBAccess();
    protectedvoid Page_Load(object sender,EventArgs e)
    {
        if (!Page.IsPostBack)
        { BindGrid(); }
    }
    protectedvoid AspNetPager1_PageChanged(object sender,EventArgs e)
    { BindGrid();
    }
    publicvoid BindGrid()
    {
        this.AspNetPager1.RecordCount =Int32.Parse(db.GetAllCount().ToString());
        int pageIndex =this.AspNetPager1.CurrentPageIndex - 1;
        int pageSize =this.AspNetPager1.PageSize = 20;
        Repeater1.DataSource = db.GetCurrentPage(pageIndex, pageSize);
        Repeater1.DataBind();
    }
三、DBAccess.cs页面的代码
using System.Data.SqlClient;
publicclassDBAccess
{
    privateSqlConnection con;
    privatestring DBName ="tongjinet";
    //创建连接对象并打开
    publicvoid Open()
    {
        if (con ==null)
            con = new SqlConnection("server=(local);uid=sa;pwd=sql;database=" + DBName);
        if (con.State ==ConnectionState.Closed)
            con.Open();
    }
    //创建一个命令对象并返回该对象
    publicSqlCommand CreateCommand(string sqlStr)
    {
        Open();
        SqlCommand cmd =newSqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = sqlStr;
        cmd.Connection = con;
        return cmd;
    }
    //生成一个对象并返回该结果集第一行第一列
    publicobject GetScalar(string sqlStr)
    {
       SqlCommand cmd = CreateCommand(sqlStr);
        object obj = cmd.ExecuteScalar();
        //CommadnBehavior.CloseConnection是将于DataReader的数据库链接关联起来
        //当关闭DataReader对象时候也自动关闭链接
        return obj;
    }
    //执行数据库查询并返回一个数据集 [当前页码,每页记录条数]
    publicDataSet GetCurrentPage(int pageIndex,int pageSize)
    {
        //设置导入的起始地址
        int firstPage = pageIndex * pageSize;
        string sqlStr ="select * from outputsell order by osid desc";
        SqlCommand cmd = CreateCommand(sqlStr);
        DataSet dataset =newDataSet();
        SqlDataAdapter dataAdapter =newSqlDataAdapter(cmd);
        dataAdapter.Fill(dataset, firstPage, pageSize,"outputsell");
        cmd.Dispose();
        Close();
        dataAdapter.Dispose();
        return dataset;
    }
    //获得查询数据的总条数
    publicobject GetAllCount()
    {
        string sqlStr ="select count(*) from outputsell";
        object obj = GetScalar(sqlStr);
        return obj;
    }
    //关闭数据库
    publicvoid Close()
    {
        if (con !=null)
        {
            con.Close();
        }
    }
    //释放资源
    publicvoid Dispose()
    {
        if (con !=null)
        {
            con.Dispose();
            con = null;
        }
    }
}
 
 
 
 
【注!!!】如果查询出来的数据为空,会显示“未将对象引用设置到对象的实例”的错误