DataList控件也玩分页(vb)
2024-07-21 02:15:39
供稿:网友
今天开发了一个系统。需要用多一行多列去展示图片。查找了一下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">
<img style="width: 158px; height: 50px" height="50" src=http://www.163design.net/a/s/"image/left1.gif" width="165" /></td>
<td width="18" background=http://www.163design.net/a/s/"image/left.gif" rowspan="2" valign="top"><img src=http://www.163design.net/a/s/"image/left.gif" width="18" />
</td>
<td valign="top" width="583" bgcolor="#ffffff" rowspan="2">
<table><tr> <td width="560"><img src=http://www.163design.net/a/s/"images/menu_shang.gif" /></td>
</tr>
|||;
<tr><td height="517" valign="top" >
<table border=0 width=100% cellspacing=0 cellpadding=0 align=center style=font-size:9pt>
<tr>
<td width=50% >
<div align=right>共有展图
<asp:label id=lblrecordcount runat=server />张</div> </td>
</tr>
</table>
<asp:datalist id="arts_list" runat="server" cellpadding="10" repeatcolumns="4">
<itemtemplate> <a title="<%# container.dataitem("title") %> <%# container.dataitem("dtime") %>"
href="news_show1.aspx?kind_id=8&id=<%# container.dataitem("id") %>&class=<%# container.dataitem("title") %>" target="_blank"><img src=<%# container.dataitem("image") %> border="0" width="115" height="80"/> </a>
</itemtemplate>
</asp:datalist>
</td>
</tr>
</table>
<form runat=server >
<table width=100% border=0 align=center style=font-size:9pt>
<tr>
<td align=center>
<asp:linkbutton id=lbnprevpage text=上一页 commandname=prev oncommand=page_onclick runat=server />
<asp:linkbutton id=lbnnextpage text=下一页 commandname=next oncommand=page_onclick runat=server />
</td>
</tr>
</table>
</form>
</td>
<td width="10" rowspan="2" align="left" valign="top" background=http://www.163design.net/a/s/"image/right.gif"><img src=http://www.163design.net/a/s/"image/right.gif"></td>
</tr>
<tr>
<td bgcolor="#f2f2f2" height="311">
</td>
</tr>
</table>
<table height="27" cellspacing="0" cellpadding="0" width="769" border="0">
<tbody>
<tr>
<td bgcolor="#dedede">
</td>
</tr>
</tbody>
</table>
</body>
</html>