欢迎光临
我们一直在努力

ASP.NET处理数据分页(2)-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

四. 第二种分页浏览数据记录的关键步骤以及实现方法: 其实这二种分页方法在程序设计中是大同小异的,在第二种方法中,其前面的关键步骤中和第一种分页几乎相同,也是要得到浏览数据记录的总数,设定每一页要显示的数据记录个数,计算出总共有多少数据页面等等。这些步骤的实现方法可以参考第一种方法。第二种分页方法和第一种分页方法的主要区别在于数据导航的实现方法上。下列代码功能是实现第二种分页方法数据导航:  

response.write ( <p > 数据导航: )
  npageend = npage + 3
  if npageend > npagecount
  npageend = npagecount
  end if
  for i = 1 to npageend
  if i = npage then
  response.write ( <b > & i.tostring ( ) & </b > )
  else
  response.write ( <a href = & script_name & _
  ?page= & ( i ).tostring ( )  & _
   > & i.tostring ( ) & </a > )
  end if
  next
  
  if npageend < npagecount then
  response.write ( <a href = & script_name & _
  ?page= & ( npageend + 1 ).tostring ( ) & _
   >更多…</a > )
  end if  

五. 第二种分页浏览数据记录的完整源程序代码(no2.aspx):  

no2.aspx和no1.aspx在程序设计的思想和方法上大致相同,下面是no2.aspx的源程序,具体如下:  

<% @ page language = vb %>
  <% @ import namespace = system.data %>
  <% @ import namespace = system.data.oledb %>
  
  <script runat = server >
  const record_per_page   as short = 5 定义每一页显示的记录数
  private script_name as string  

sub page_load ( source as object , e as eventargs )
  script_name = getpagename ( )
  第二种方式来分页显示数据
  showrecords ( )
  end sub
  
  得到起始浏览超链接字符串
  function getpagename ( ) as string
  dim str as string
  dim pos as short
  str = request.servervariables ( script_name ).trim ( )
  pos = str.lastindexof ( / )
  if pos >= 0 then
  return str.substring ( pos + 1 )
  else
  return str
  end if
  end function
  
  private sub showrecords ( )
  dim strconn as string 定义数据连接字符串  
dim sql as string  定义sql语句
  dim odconn as oledbconnection
  dim odadapt as oledbdataadapter
  dim ds as dataset 创建dataset对象
  dim dt as datatable 创建datatable对象
  dim nreccount as integer 保存记录总数
  dim npagecount as integer 保存总共的数据页面数目
  dim npage as integer 存放要浏览当前数据页面号
  dim nstart as integer 存放当前页面的起始记录序号
  dim nend as integer 存放当前页面的终止记录序号
  dim npageend as integer 存储当前页面的最后一面的序号
  dim i as integer  
确认要浏览的页面序号
npage = convert.toint32 ( request.querystring ( page ) )
  sql = select * from tblitem
  
  创建数据连接字符串
  strconn = provider = microsoft.jet.oledb.4.0 ; & _
   data source = & server.mappath ( data.mdb ) & ; & _
   user id = ; password = ;
  try
  得到数据记录总数
  odconn = new oledbconnection ( strconn )
  odadapt = new oledbdataadapter ( sql , odconn )
  ds = new dataset
  odadapt.fill ( ds )
  dt = ds.tables ( 0 )
  nreccount = dt.rows.count
  catch e as exception
  response.write(错误信息: <b > & e.message & </b > <p > )
  nreccount = 0
  end try
  
  if nreccount > 0 then
   确定数据记录要显示的页面数
  npagecount = nreccount \ record_per_page
  if nreccount mod record_per_page > 0 then
  npagecount += 1
  end if
  
  确认浏览命令中的页面参数是否越界,如果越界则重置页面序号
  if npage < 1  then
  npage = 1  
end if
  if  npage > npagecount then
  npage = npagecount  
end if
  
  response.write ( 总共有数据记录 & nreccount.tostring ( ) & 条 & 。<br > )
  response.write( <p > <b >第二种分页显示为:</b > <p > )
  
  确认当前页面的开始记录和终止记录
  nstart = record_per_page *  ( npage – 1 )
  nend = nstart + record_per_page – 1
  if nend > nreccount – 1 then
  nend = nreccount – 1
  end if
  在屏幕中输出记录
  for i = nstart to nend
  response.write ( dt.rows ( i ) ( itemname ) & <br > )
  next
  end if
  response.write ( <p > 数据导航: )
  npageend = npage + 3
  if npageend > npagecount
  npageend = npagecount
  end if
  for i = 1 to npageend
  if i = npage then
  response.write ( <b > & i.tostring ( ) & </b > )
  else
  response.write ( <a href = & script_name & _
  ?page= & ( i ).tostring ( )  & _
   > & i.tostring ( ) & </a > )
  end if
  next
  
  if npageend < npagecount then
  response.write ( <a href = & script_name & _
  ?page= & ( npageend + 1 ).tostring ( ) & _
   >更多…</a > )
  end if
  end sub
  </script >  

本文介绍的这二种分页浏览记录类型虽然采用的数据库都是本地数据库,但对其他类型的数据库也是一样适用的,这只需要修改一下数据连接字符串就可以实现了,譬如如果采用了sql server数据库。此sql server数据库服务器是server1,数据库是data,用户名为缺省的sa,没有设定密码。只需要把上面二段程序中的字符串strconn变换成:  

strconn = provider = sqloledb.1 ; persist security info = false ; user id = sa ; initial catalog = data ; data source = server1   

就可以实现了。  

六. 总结:  

本文介绍的二种分页浏览数据记录方法在asp.net数据库编程方面是非常有用的,因为在数据处理方面,分页显示记录比起其他的一些处理,譬如:数据修改、删除等都要难些。希望上面的这些内容对你利用asp.net开发数据库程序有所帮助。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ASP.NET处理数据分页(2)-.NET教程,Asp.Net开发
分享到: 更多 (0)

相关推荐

  • 暂无文章