【转】AspNetPager分页控件使用方法例子

来源:互联网 发布:足球数据分析网站 编辑:程序博客网 时间:2024/04/30 23:58
一、前台显示界面代码Default.aspx(注意,代码运行环境是VS.2005)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default4" %>
<%@ 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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table border=1>
       <asp:Repeater ID="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: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>
              
<%--<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 = new DBAccess();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        { BindGrid(); }
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    { BindGrid();
    }
    public void 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;
public class DBAccess
{
 
    private SqlConnection con;
    private string DBName = "tongjinet";
 
    //创建连接对象并打开
    public void Open()
    {
        if (con == null)
            con = new SqlConnection("server=(local);uid=sa;pwd=sql;database=" + DBName);
        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 outputsell order by osid desc";
        SqlCommand cmd = CreateCommand(sqlStr);
        DataSet dataset = new DataSet();
        SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
        dataAdapter.Fill(dataset, firstPage, pageSize, "outputsell");
        cmd.Dispose();
        Close();
        dataAdapter.Dispose();
        return dataset;
    }
    //获得查询数据的总条数
    public object GetAllCount()
    {
        string sqlStr = "select count(*) from outputsell";
        object obj = GetScalar(sqlStr);
        return obj;
    }
 
    //关闭数据库
    public void Close()
    {
        if (con != null)
        {
            con.Close();
        }
    }
    //释放资源
    public void Dispose()
    {
        if (con != null)
        {
            con.Dispose();
            con = null;
        }
    }
}
 
AspNetPager分页控件使用方法(不使用DBAccess.cs文件)(二)
 
AspNetPager分页控件使用方法(不使用DBAccess.cs文件)
一、前台显示界面代码Default.aspx(注意,代码运行环境是VS.2005)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default4" %>
<%@ 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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table border=1>
       <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
        <tr>
        <td><%#DataBinder.Eval(Container.DataItem, "city_id")%></td>
        <td><%#DataBinder.Eval(Container.DataItem, "city_name")%></td>
        </tr>
        </ItemTemplate>
        </asp:Repeater>
    </table>
 
 <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>
二、Default.aspx.cs页面的代码
using System.Data.SqlClient;
public partial class Default : System.Web.UI.Page
{
    //SqlConnection con = connsql.ZYJCreateConnection();
    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 = connsql.ZYJCreateConnection();
        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 city order by city_id desc";
        SqlCommand cmd = CreateCommand(sqlStr);
        DataSet dataset = new DataSet();
        SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
        dataAdapter.Fill(dataset, firstPage, pageSize, "city");
        cmd.Dispose();
        Close();
        dataAdapter.Dispose();
        return dataset;
    }
    //获得查询数据的总条数
    public object GetAllCount()
    {
        string sqlStr = "select count(*) from city";
        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();
    }
}
 
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 海信电视怎么安装第三方软件怎么办 网上买票的时候不想用学生票怎么办 南方公园完整破碎讲话嘴不动怎么办 同一个安全员被锁在两个项目怎么办 家长拖欠家教老师的课时费怎么办 合同没到期房东不退押金怎么办 租房合同没到期房东要违约怎么办 档案存放费交了一年的延期怎么办 天津房子卖了户口没地方迁怎么办 中国到美国读计算机硕士签证怎么办 在江苏大学去德国读研怎么办? 宿舍上下铺的床一动就响怎么办 自助取款机存款忘打印了凭条怎么办 高一新生跨省转学籍怎么办 网上买票不小心买成了学生票怎么办 做横幅标语字打出来老是歪的怎么办 创业板股票暂停上市后钱怎么办 新股东入股公司之前的亏损怎么办 目前公司账面亏损有人要入股怎么办 土地确权后没有土地的人怎么办 老板不发工资跑路了怎么办 别人登录了我的美团账号怎么办 月嫂家政公司快坚持不下去了怎么办 华硕飞马4a手机发热怎么办 超级必发指数手机页面没曲线怎么办 唐小僧理财暴雷了投资者该怎么办 教师资格证面试准考证号忘了怎么办 初中学校说处分不给毕业证怎么办? 自动档一键打火的车没电了怎么办 物流代收货款一直拿不到钱怎么办 丰巢快递柜收不到验证码怎么办 拼多多三级惩罚下架3天怎么办 在万达买的衣服穿一次烂了怎么办 内蒙古对于没有地的农民改怎么办 微信号码重新注册后回零钱怎么办 安卓系统文件苹果手机打不开怎么办 课题必须发表论文吗?查重怎么办 学信网学籍绑定输错5次怎么办 大学学校图书馆借的书丢了怎么办 借阅机里的图书不显示书名怎么办 苹果6s锁屏密码忘了怎么办