- 爱易网页
-
ASP.NET教程
- DataList控件也玩分页(vb)
日期:2014-04-26 浏览次数:20595 次
今天开发了一个系统。需要用多一行多列去展示图片。查找了一下DATAGIRD的属性。发现其没有重复列的功能。于是我使用了datalist的RepeatColumns="4"列重复功能,却又发现DATALIST没有分页功能。怎么办?于是写了下面的关于的分页程序。发出来希望对正在学ASP.NET程序的朋友能有所帮作。
下面是全部原代码,这里我使用了DataAdapter与DataSet组合,在开始程序的时候,我们首先要熟悉一下ASP.NET 中 DataAdapter,DataSet和ViewState的一些属性和使用方法:http://www.bookd.net/info/1461.htm ;
(本程序在.Net Framework Beta 2下测试通过)
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="gb2312" %>
<%@ register tagprefix="head" tagname="menu" src="menu/head.ascx" %>
<%@ import Namespace="system.data" %>
<%@ import Namespace="system.data.sqlclient" %>
<script runat="server">
dim db1 as string = "ietop_article"
Dim myconn as new sqlconnection("uid=sa;password=;database=ietop;server=(local)") Dim Pagesize,Pagecount,RecordNum,CurrentPage as integer Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
每页显示的个数 */
Pagesize=20
联接数据库 */
myconn.open()
计算符合该新闻系统的新闻条数 */
dim strsql as string = "select count(*) as Id from ["& db1 &"] where kind_id="&request("id")&""
dim MyComm as sqlcommand = new sqlCommand(strsql,myconn)
Dim dr as sqlDataReader = MyComm.ExecuteReader()
Dim intCount as integer
if dr.Read() then
intCount =dr("ID")
else
intCount = 0
end if
dr.Close()
myconn.close()
lblRecordCount.Text =intcount
RecordNum=intcount
'计算总共有多少页 */
if recordnum mod pagesize=0 then
PageCount = RecordNum\PageSize
else
PageCount = RecordNum\PageSize +1
end if
ViewState(PageCount) = PageCount If Not IsPostBack then
CurrentPage = 0
ViewState("PageIndex") = 0
ListBind()
end if End Sub Sub Page_onClick(sender As Object, e As CommandEventArgs)
CurrentPage = ViewState("PageIndex")
PageCount = ViewState(PageCount)
Dim cmd as string = e.CommandName
select case cmd
case "next"
if CurrentPage<(PageCount-1) then CurrentPage=+1
case "prev"
if CurrentPage>0 then CurrentPage-=1
end select ViewState("PageIndex") = CurrentPage
ListBind() end sub
sub listbind()
Dim StartIndex as integer
StartIndex = CurrentPage*PageSize
Dim strSel as string = "select id,title,dtime,writer,image from ["& db1 &"] where kind_id="&request("id")&" order by id desc"
Dim ds as DataSet = new DataSet()
Dim MyAdapter as sqlDataAdapter = new sqlDataAdapter(strSel,myconn)
MyAdapter.Fill(ds,StartIndex,PageSize,"Result")
'读取分页数据:使用DataSet,DataAdapter.Fill(ObjDataSet,开始数,结尾数,"数据")
arts_list.DataSource = ds.Tables("result").DefaultView
arts_list.RepeatDirection = RepeatDirection.Horizontal
arts_list.DataBind() lbnNextPage.Enabled = true
lbnPrevPage.Enabled = true
if(CurrentPage=(PageCount-1)) then lbnNextPage.Enabled = false
if(CurrentPage=0) then lbnPrevPage.Enabled = false end sub </script>
<html>
<head>
<title>创艺家装</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<link href="css/main.css" type="text/css" rel="stylesheet" />
</head>
<body>
<head:menu id="head1" runat="server"/>
<table cellspacing="0" cellpadding="0" width="769" border="0">
<tbody>
<tr>
<td width="158" bgcolor="#f2f2f2">