1.首先在数据库中建立一个视图(在aspx中sql查询语句是view_student不是student),在视图里创建
create view view_student--创建视图
as row_number 行号 一条数据是一行 分页功能要根据行数运算
select *,row_number() over(order by stuNo desc) as rowindex from Student
创建一个名为rowindex的字段,row_number() 函数是返回行号,要与over连用
2.在项目中创建一个studentlist.aspx.repeateList控件用户重复的数据简单化,
-----------在studentList.aspx 中的代码
<table>
<tr>
<td>序号</td>
<td>学号</td>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>操作</td>
</tr>
<asp:Repeater ID="rptStudent" runat="server">
<ItemTemplate>Repeater如果是一个表格,那么它就有多个行组成的,而它的每一行显示的数据,就是由ItemTemplate控制的
<tr>
<td><%#Eval("rowindex")%></td>--------------<%#Eval("字段")%>用于绑定数据
<td><%#Eval("stuNo")%></td>
<td><%#Eval("stuName")%></td>
<td><%#Eval("sex")%></td>
<td><%#Eval("birthday")%></td>
<td>操作</td>
</tr>
</ItemTemple>
</asp:Repeater>
</table>
--------------------在studentList.aspx.cs 中的代码
在直观的浏览器中分页要理解的是多少页,一页有多少行(pageindex,pagesize)pageindex当前页码pagesize当前页码有几条数据
如何将数据库中几百万的数据引进来,分开要用什么方法,
在dal层中定义方法-----------------------------------------------------------------------------------
public int GetRecordCount()//获取学生总数,方便用于分多少页,一页包括多少数据,方便计算
{
string sql="select count(*) from Student";
return (int)Sqlhelper.GetSingleData(sql);
}
public DataSet GetPageindexData(int pageindex,int pagesize)将总数分成几页,一页几条数据,数据成“团”出现,用set返回
{
从view_Student中获取数据
string sql="select * from view_Student where rowindex between @startrowindex and @endrowindex";
sqlparameter[] sps={
new sqlparameter("@starrowindex",(pageindex-1)*pagesize+1),
new sqlparameter("@endrowindex",pageindex*pagesize)
};
return sqlhelper.Getdataset(sql,sps);
}
在DAL中定义方法,在BLL中调用一下方法
-------------在BLL中定义一下方法
public int GetRecordCount()
{
return stuDal.GetRecordCount(); 要现在DAl中实例化一下Student,才可以调用一下stuDal.GetRecordCount();
}
public DataSet GetPageindexData(int pageindex, int pagesize)
{
return stuDal.GetPageindexData(pageindex,pagesize);
}
---------------------------------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindRepeater(1,3);//(a,b)a代表第几页,b代表有几个数据
}
}
public void bindRepeater(int pageindex,int pagesize)
{
Student stubll=new Student();
rptStudent.DataSource =stubll.GetpageindexData(pageindex, pagesize);控件的数据来源
rptstudent.DataBind();绑定数据
}
----------------------------
create view_student create view View_Student as select *,Row_Number(),over(order by StuNo desc) as rowindex from Student
as
select *,Row_Number(), over() as rowindex from student
--------------------------------------------------------------------------------------------------
将出生日期装换成年龄
public static GetAge(object birthday)//需要传参数
{
int NowYear=DateTime.Now.year;
int OldYear=((DateTime)birthday).Year;
return (NoweYear-OldYear).ToString();
}
在.aspx 的页面绑定数据的页面<td><%#GetAge(Eval("birthday"))%></td>这样调用方法,把数据绑定上面
---------------------------------------------------------------------------------------------------------
上下页 需要考虑的是 一共多少数据,每个页面存放多少数据,
当pageindex<=1时,builder.Append(" 首页 下一页");
else
builder.Append(<a href='href="StudentList.aspx?pindex=1"'>首页</a>);变成超链接
builder.Append(<a href='href="StudentList.aspx?pindex='"+(pindex-1)+"'"'>上一页页</a>);变成超链接
当pageindex=pageCount时
if(pageindex==pageCount)
{
builder.Append(" 下一页 末页");
}
else
{
builder.Append("<a href='StudentList.aspx?pindex=" + (pageindex + 1) + "'>下一页</a>");
builder.Append("<a href='StudentList.aspx?pindex="+pageCount+"'>末页<a/>");
}
!!----------!!! Pager = builder.ToString();
<%=%> 里面放的变量名 <p>Welcome to Beginning ASP.NET 3.5 on <%= DateTime.Now.ToString() %></p>
Welcome to Beginning ASP.NET 3.5 on 2009-11-10 15:53:08
<%#%> 这里是数据的绑定 只能用在数据绑定控件中
----------------------------------------------------------------------------------------------------------
public string GetPager(ref int pageindex, int pagecount)
{
if (pageindex < 1)//如果页码<1,则当前页码为第一页
{
pageindex = 1;
}
if (pageindex > pagecount)
{
pageindex = pagecount;
}
StringBuilder sb = new StringBuilder();
if (pageindex == 1)
{
sb.Append(" 首页 上页");
}
else
{
sb.Append(" <a href='javascript:LoatData(1)'>首页</a>");
sb.Append(" <a href='javascript:LoatData(" + (pageindex - 1) + ")'>上页</a>");
}
if (pageindex == pagecount)
{
sb.Append(" 下页 末页 ");
}
else
{
sb.Append(" <a href='javascript:LoatData(" + (pageindex + 1) + ")'>下页</a> ");
//sb.Append(" <a herf='javascript:LoatData(" + (pagecount) + ")'>末页</a>");
sb.Append(" <a href='javascript:LoatData(" + (pagecount) + ")'>末页</a>");
}
return sb.ToString();
}
<div class="page_nav"> <%=Pager%> </div>public string Pager { set; get; }int RecordCount, PageCount, PageSize, PageIndex;protected void Page_Load(object sender, EventArgs e){//先获数据库里取有多少条数据RecordCount = ProductBLL.GetRecordCount();PageSize = 2;PageCount=(int)Math.Ceiling(RecordCount*1.0/PageSize);PageIndex =1;//定义开始从第一页开始if (!string.IsNullOrEmpty(Request["pindex"])){ PageIndex=Convert.ToInt32(Request["pindex"]);}if (!IsPostBack){//下方注释不能删除//ProductList = ProductBLL.GetProduct();//Repeater1.DataSource = ProductBLL.GetProduct();//Repeater1.DataBind();ViewState["pindex"] = PageIndex;DataTable dt = Binder(PageIndex,PageSize);Repeater1.DataSource = dt;Repeater1.DataBind();GetPager(PageIndex, PageCount);}}public void GetPager(int pageindex, int pagesize){if (pageindex < 1){ PageIndex=1;}if (pageindex > PageCount){pageindex = PageCount;}StringBuilder sb = new StringBuilder();if (pageindex < 1){sb.Append("首页~~~上一页");}else{sb.Append("<a href='?pindex=1'>首页~~~</a>");sb.Append("<a href='Product.aspx?index="+(pageindex-1)+"'>上一页</a>");}if (pageindex == PageCount){sb.Append("~~~下一页 末页");}else{sb.Append("<a href='Product.aspx?pindex=" + (pageindex + 1) + "'>~~~下一页~~~</a>");sb.Append("<a href='?pindex="+PageCount+"'>末页</a>");}Pager = sb.ToString();}