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();
    }
}
 
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值