Asp.Net教程,WinForm教程,Asp.Net MVC,vs2008教程,vs2010教程,Silverlight技术,源码下载,Asp.Net视频教程
全站热门标签
vs2010 Silverlight 存储过程 水晶报表 ADO.NET JavaScript LINQ AjaxPro DataGridView 面向对象 Extjs GridView XML DevExpress HTML教程 Oracle jQuery 分页 Prototype教程 GDI+ Visual C++2010 MySQL Office2010 WPF MVC Dojo WCF4.0 VB.NET Sql2005 textbox cookie WCF WinForm Discuz!NT SQL经典语句 T-SQL checkbox ASPxGridView F# asp.net SQL VS2008新特性 DropDownList Access TreeView Ajax VS2008 页面执行时间 Flex 字符串 回调 VB2005 DataSet C#时间 ASP.NET性能优化 用户在线检测
尚未分类 LINQ教程 Enterprise技术 性能优化/调试 水晶报表与打印 安全与加密 图形图像 文件处理 基础教程 Web Services 内置对象 控件示例 正则表达式\采集 ADO.NET 缓存\泛型\线程 XML技术 Url重写\静态页 vs2008综合教程
当前位置: 主页 > ASP.NET教程 > 尚未分类 >

ASP.NET 2.0 中AspNetPager.dll控件的分页方法操作方法

时间:2010-06-30 11:12来源:未知 作者:admin 点击:

前台显示界面代码Default.aspx[代码运行环境:VS.2005]
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ 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>用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,"job_id")%></td>
        <td><%#DataBinder.Eval(Container.DataItem,"job_desc")%></td>
        <td><%#DataBinder.Eval(Container.DataItem,"min_lvl")%></td>
        <td><%#DataBinder.Eval(Container.DataItem,"max_lvl")%></td>
        </tr>
        </ItemTemplate>
        </asp:Repeater>
    </table>
       <!--  <asp:DataGrid ID="DataGrid1" runat="server">
        </asp:DataGrid>-->
        <webdiyer:AspNetPager ID="AspNetPager1" runat="server" Width="733px" FirstPageText="第一页" LastPageText="最后一页" NextPageText="下一页" PrevPageText="上一页" Font-Names="Arial" BackColor="#F8B500" AlwaysShow="true"  ShowInputBox="Always" SubmitButtonText="跳转" SubmitButtonStyle="botton" OnPageChanged="AspNetPager1_PageChanged" >
              </webdiyer:AspNetPager>
    </div>
    </form>
</body>
</html>

//Default.aspx页面的代码

    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 =5;
        Repeater1.DataSource = db.GetCurrentPage(pageIndex, pageSize);
        Repeater1.DataBind();
    }

//前台已经OK了,就差后台数据库连接及分页需要的方法,本项目数据库示例为PUBS数据库[安装SQL SERVER就有的],分页的表为jobs表

using System.Data.SqlClient;

public class DBAccess
{
    private SqlConnection con;
    private string DBName = "pubs";

    //创建连接对象并打开
    public void Open()
    {
        if (con == null)
            con = new SqlConnection("server=(local);uid=sa;pwd=sa;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 jobs order by job_id desc";
        SqlCommand cmd = CreateCommand(sqlStr);
        DataSet dataset = new DataSet();
        SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
        dataAdapter.Fill(dataset,firstPage,pageSize,"jobs");
        cmd.Dispose();
        Close();
        dataAdapter.Dispose();
        return dataset;
    }

    //获得查询数据的总条数
    public object GetAllCount()
    {
        string sqlStr = "select count(*) from jobs";
        object obj = GetScalar(sqlStr);
        return obj;
    }
 
    //关闭数据库
    public void Close()
    {
        if (con != null)
        {
            con.Close();
        }
    }

    //释放资源
    public void Dispose()
    {
        if (con != null)
        {
            con.Dispose();
            con = null;
        }
    }

(责任编辑:admin)
Tags:分页 AspNetPager
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价