asp中AspNetPager分页控件的sql和存储过程的用法

基于班上的需求简单的使用AspNetPager分页控件的两种方法

第一步首先去网上下载AspNetPager插件

第二步将控件放到项目的bin问件下
在这里插入图片描述
将控件导入到项目中去:事项右击工具箱中的选择项
在这里插入图片描述

点击浏览将bin文件夹下的AspNetPager导入进去在这里插入图片描述
有时会出现后台点不出包可以在项目中添加引用后点击浏览再次将AspNetPager导入在这里插入图片描述

将控件拖放到页面后我们需要知道AspNetPager插件的三个属性
RecordCount是数据的总行数,它要知道你查的数据总共有多少条
PageSize是页面显示行数,是一页中显示多少条数据
CurrentPageIndex是当前页面的索引,指页码

三层中需要DAL层中的代码

//查询总行数得到RecordCount数据的总行数
public static int selectcountStu()
{
string sql = string.Format(" select count(*) from stuInfo ");
DataTable dt = DBHelper.GetDataTable(sql);
if (dt!=null)
{
return int.Parse(dt.Rows[0][0].ToString());
}
else
{
return 0;
}
}

///
/// 查询学生信息 Top就是PageSize指一页显示的行数,
///假如你查询的是第三页的数据,那你就得除去前两页的数据,所以({1}-1){0})就是当前页面索引减去1再乘以一页显示的数据得到的是前面两页的数据 not in 是指不在这些数据中,所以这条语句的意思就是除去前两页的数据查询第三页的数据
///

///
public static List selectAllDAl(int PageSize,int currtIndex)
{
string sql = string.Format( @" select top {0} *
from stuInfo s,classInfo c,DegreeInfo d where s.classId=c.classId and s.DegreeID=d.DegreeID and stuID
not in (select top (({1}-1){0}) stuID from stuInfo )order by s.stuID", PageSize, currtIndex);

        DataTable dt = DBHelper.GetDataTable(sql);
        List<StuInfoModel> list = new List<StuInfoModel>();
        if (dt.Rows.Count > 0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                StuInfoModel stu = new StuInfoModel();
                stu.StuID = dr["stuID"].ToString();
            
                stu.StuName = dr["stuName"].ToString();
                stu.StuGender = dr["stuGender"].ToString();
               
               
                stu.StuImg = dr["stuImg"].ToString();
                stu.StuBirthday = DateTime.Parse(dr["stuBirthday"].ToString());
            
                stu.DegModel.DegreeName = dr["DegreeName"].ToString();
                stu.ClassModel.ClassName = dr["className"].ToString();
                list.Add(stu);
            }
            return list;
        }
        else
        {
            return null;
        }
    }

Bll层就是直接调用

//查询总行数
public static int selectcountStu()
{
return StuClassDAL.selectcountStu();
}
///
/// 查询学生信息
///
/// true/false
public static List selectAllBLL(int pageindex, int currtIndex)
{
return StuClassDAL.selectAllDAl(pageindex,currtIndex);
}

UI层

public void bind()
{
//获取到RecordCount 中的数据的总行数
this.AspNetPager1.RecordCount = StuAllBLL.selectcountStu();
//传参数一页PageSize显示的数据行数和CurrentPageIndex当前页面的索引
this.Repeater1.DataSource = StuAllBLL.selectAllBLL(this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex);
this.Repeater1.DataBind();
}
在AspNetPager的PageChanged事件中调用函数
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
bind();
}

存储过程的用法

打开AspNetPager1任务
点击存储过程生成工具

在这里插入图片描述
在这里插入图片描述
注册它会自己给你三个变量
@startIndex int,//开始
@endIndex int,//结束
@docount bit
存储过程的查询startIndex 是开始到endIndex 结束
也就是查询第多少条到多少条之间的数据
如果想加其他的变量的话点击注册

点击生成后是这样的
create procedure proc_select
(@startIndex int,
@endIndex int,
@docount bit)
as

if(@docount=1)
select count(*) from stuInfo s,classInfo c,DegreeInfo d where s.classid=c.classid and s.degreeid=d.degreeid
else
begin
with temptbl as (
SELECT ROW_NUMBER() OVER (ORDER BY stuID desc)AS Row, * from stuInfo s,classInfo c,DegreeInfo d where s.classid=c.classid and s.degreeid=d.degreeid)
SELECT * FROM temptbl where Row between @startIndex and @endIndex
end

因为@docoun在后面没有用到所以我将其删掉了,根据我的需求改成了
这里说一下如果你查询多表的话两个表有着相同的字段的话temptbl 会提示你为它指定了多个相同的字段这个时候你就需要将你要查的子段放到Rows后面,过滤掉相同的字段

create procedure proc_select
(@startIndex int,
@endIndex int
)
as
begin
with temptbl as (
SELECT ROW_NUMBER() OVER (ORDER BY stuID desc)AS Row,s.stuID,s.stuQQ,s.stuPhone,s.stuAddress,s.stuHobby, s.stuName,s.stuGender,s.stuBirthday,c.className,d.DegreeName,s.stuImg,c.classId,d.DegreeID
from stuInfo s,classInfo c,DegreeInfo d where s.classid=c.classid and s.degreeid=d.degreeid)
SELECT * FROM temptbl where Row between @startIndex and @endIndex
end

同时DBHelper中

需要有查询存储过程的方法
///
/// 获取DataSet数据集(通过存储过程)
///
/// CommandType类型
/// 存储过程名
/// 参数
public static DataSet GetDataSetByProc(CommandType type, string procName, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = procName;
cmd.CommandType = type;
cmd.Parameters.AddRange(parameters);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataSet ds = new DataSet())
{
try { da.Fill(ds); return ds; }
catch (SqlException)
{
return null;
}
finally
{
conn.Close();
}

                    }
                }
            }
        }
    }

**

DAL层中

**
也是一个查询总行数,只是把后面的查询改成了存储过程
存储过程的查询pagesize是开始到endpage结束
也就是查询第多少条到多少条之间的数据
//查询总行数
public static int selectStuCount()
{
string sql = string.Format(“select count(*) from stuInfo”);
DataTable dt = DBHelper.GetDataTable(sql);
if (dt!=null)
{
return int.Parse(dt.Rows[0][0].ToString());
}
else
{
return 0;
}
}
///
/// 查询学生信息
///
///
///
public static List selectAllDAl(int pagesize,int endpage)
{
SqlParameter[] paremeters =
{
new SqlParameter("@startIndex",pagesize),
new SqlParameter("@endIndex",endpage)
};
//proc_select是存储过程的名字
DataSet ds = DBHelper.GetDataSetByProc(CommandType.StoredProcedure, “proc_select”,paremeters);
List list = new List();
DataTable dt = ds.Tables[0];
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
StuInfoModel stu = new StuInfoModel();
stu.StuID = dr[“stuID”].ToString();
stu.ClassId = dr[“classId”].ToString();
stu.StuName = dr[“stuName”].ToString();
stu.StuGender = dr[“stuGender”].ToString();
stu.StuHobby = dr[“stuHobby”].ToString();
stu.StuAddress = dr[“stuAddress”].ToString();
stu.StuPhone = dr[“stuPhone”].ToString();
stu.StuImg = dr[“stuImg”].ToString();
stu.StuBirthday = DateTime.Parse(dr[“stuBirthday”].ToString());
stu.StuQQ = dr[“stuQQ”].ToString();
stu.DegreeID = int.Parse(dr[“DegreeID”].ToString());
stu.DegModel.DegreeName = dr[“DegreeName”].ToString();
stu.ClassModel.ClassName = dr[“className”].ToString();
list.Add(stu);
}
return list;
}
else
{
return null;
}
}

BLL层

//查询总行数
public static int selectStuCount()
{
return StuClassDAL.selectStuCount();
}
///
/// 查询学生信
///
/// true/false
public static List selectAllBLL(int pagesize, int endpage)
{
return StuClassDAL.selectAllDAl(pagesize,endpage);
}

UI层

 //分页数据显示绑定
    public void Bind()
    {
        this.AspNetPager1.RecordCount = StuAllBLL.selectStuCount();//查询总数据数行数
        this.Repeater1.DataSource = StuAllBLL.selectAllBLL(AspNetPager1.PageSize*(AspNetPager1.CurrentPageIndex-1)+1,AspNetPager1.CurrentPageIndex*AspNetPager1.PageSize);
        //计算从第多少条开始到多少条结束
        this.Repeater1.DataBind();
    }
    //分页点击事件调用函数
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        Bind();
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、支持通过Url进行分页AspNetPager除提供默认的类似于DataGrid和GridView的PostBack分页方式外,还支持通过Url进行分页,象大多数asp程序分页一样, Url分页方式允许用户通过在浏览器地址栏输入相应的地址即可直接进入指定页面,也可以使搜索引擎搜索到所有分页的页面的内容,因此具有用户友好和搜索引擎友好的优点,关于Url分页与PostBack分页方式的差异,请参考Url与PostBack分页方式的对比。 2、支持Url分页方式下的Url重写(UrlRewrite)功能 Url重写技术可以使显示给用户的Url不同于实际的Url,Url重写技术被广泛应用于搜索引擎优化(SEO)、网站重组后重定向页面路径以及提供用户友好的Url等方面, AspNetPager支持Url重写技术使您可以自定义分页导航的Url格式,实现Url重写; 3、支持使用用户自定义图片做为导航元素: 您可以使用自定义的图片文件做为分页控件的导航元素,而不仅仅限于显示文字内容。 4、功能强大灵活、使用方便、可定制性强: AspNetPager分页控件的所有导航元素都可以由用户进行单独控制,从6.0版起,AspNetPager支持使用主题(Theme)与皮肤(Skin)统一控件的整体样式,配合asp.net 2.0的DataSource控件AspNetPager只需要编写短短几行代码,甚至无需编写任何代码,只需设置几个属性就可以实现分页功能。 5、增强的 Visual Studio 2005、Visual Studio 2008和Visual Studio 2010设计时支持 增强的设计时支持使控件在设计时更加直观,易于使用,开发快捷方便。 6、兼容IE6.0+及FireFox 1.5+等浏览器 7、丰富而完整的控件文档和示例项目: 控件附带的完整的帮助文档及示例项目能够帮助您快速上手,熟悉AspNetPager控件使用,您还可以通过给作者留言以及论坛提问等方式解决控件使用遇到的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值