ASP用netpager做分页

项目:GiftExchange   时间:180725

一、导入netpager控件

自行下载控件后,将其放入项目UI层的bin包(点击解决方案资源管理器下方的第二个图标,显示所有即可见)中,如下图:

774135bc8505a218b05931070b32a23654c.jpg

我们再把AspNetPager.dll添加到VS工具箱的.net组件中,在视图中找到工具箱,右键工具箱下的标准,点击选中项(而不是添加选项)

                                      

  进入如下页面,在.net framework组件下,点击浏览,找到AspNetPager.dll存放的地方(也就是刚刚放入的UI层下的bin包中),然后就自动添加了,下图是已经添加好的,然后再UI层将NetPager添加引用  ,然后就可以像其他组件一样使用NetPager 了

                                     

Test.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="UI.DataCenter.Activity.Test" %>

<%@ 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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="text-align:center;"> 
       <%-- <webdiyer:AspNetPager ID="AspNetPagerAskAnswer" runat="server" 
        AlwaysShow="True" FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" 
        onpagechanged="AspNetPagerAskAnswer_PageChanged" PrevPageText="上一页" 
        PageSize="10"> 
        </webdiyer:AspNetPager> 

<br /> 

        <webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow="True" 
            FirstPageText="首页" LastPageText="尾页" 
            onpagechanged="AspNetPager1_PageChanged" NextPageText="下一页" PrevPageText="上一页">
        </webdiyer:AspNetPager>--%>


        <table border=1 align="center">

            <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate>
            <tr>
                <td colspan="17" style="background-color: #394a6a; color: White" align="center">
                    <b>城市列表</b>
                </td>
            </tr>
            <tr>
            
                <td bgcolor="#ececec">
                    条数
                </td>
                <td bgcolor="#ececec">
                    省城ID
                </td>
                <td bgcolor="#ececec">
                    城市名
                </td>
                <td bgcolor="#ececec">
                   状态
                </td>
            </tr>
            </HeaderTemplate>
        <ItemTemplate>
        <tr>
        <td><%#Eval("id")%></td>
        <td><%#Eval("Provinceid")%></td>
        <td><%#Eval("city")%></td>
        <td><%#Eval("Flag")%></td>
        </tr>
        </ItemTemplate>
        </asp:Repeater>
    </table>
 <webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow="True" 
            FirstPageText="首页" LastPageText="尾页" 
            onpagechanged="AspNetPager1_PageChanged" NextPageText="下一页" PrevPageText="上一页">
        </webdiyer:AspNetPager>
 <%--<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>--%>
    </form>
</body>
</html>

Test.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace UI.DataCenter.Activity
{
    public partial class Test : System.Web.UI.Page
    {
        DAL.DBAccess db = new DAL.DBAccess();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            { bind(); }
        }
   
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            bind();
        }

        private void bind()
        {
            this.AspNetPager1.RecordCount = Int32.Parse(db.GetAllCount().ToString());
            int pageIndex = this.AspNetPager1.CurrentPageIndex - 1;
            int pageSize = this.AspNetPager1.PageSize = 10;
            Repeater1.DataSource = db.GetCurrentPage(pageIndex, pageSize);
            Repeater1.DataBind();
        } 
    }
}

Dal层(因为在UI层引用了dal,所以可以之间调用dal层方法)

DBAcess.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class DBAccess
    {
        private SqlConnection con;
        private string TableName = "City";
        static string ConnStr = "Data Source=192.168.1.29;Initial Catalog=meraki;Persist Security Info=True;User ID=******;Password=*******";
        //创建连接对象并打开
        public void Open()
        {
            if (con == null)
                con = new SqlConnection(ConnStr);
            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 id asc";
            SqlCommand cmd = CreateCommand(sqlStr);
            DataSet dataset = new DataSet();
            SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
            dataAdapter.Fill(dataset, firstPage, pageSize, TableName);
            cmd.Dispose();
            Close();
            dataAdapter.Dispose();
            return dataset;
        }
        //获得查询数据的总条数
        public object GetAllCount()
        {
            string sqlStr = "select count(*) from " + TableName;
            object obj = GetScalar(sqlStr);
            return obj;
        }

        //关闭数据库
        public void Close()
        {
            if (con != null)
            {
                con.Close();
            }
        }
        //释放资源
        public void Dispose()
        {
            if (con != null)
            {
                con.Dispose();
                con = null;
            }
        }
    }
}

DBAccess中需要修改的是 数据库名,表名以及sql语句即:

private string DBName = "Dktest";
private string TableName = "City";
static string ConnStr = "Data Source=192.168.1.29;Initial Catalog=meraki;Persist Security Info=True;User ID=sa;Password=sa123456";

string sqlStr = "select * from City order by id asc";

 

 

数据绑定也可以修改成自己想要的,我这里用的是asp的Repeater控件,所以是这样的

 

转载于:https://my.oschina.net/8824/blog/1862682

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值