DataList控件也玩分页(vb)

发表于:2007-06-30来源:作者:点击数: 标签:
今天 开发 了一个系统。需要用多一行多列去展示图片。查找了一下DATAGIRD的属性。发现其没有重复列的功能。于是我使用了datalist的RepeatColumns=4列重复功能,却又发现DATALIST没有分页功能。怎么办?于是写了下面的关于的分页程序。发出来希望对正在学ASP.N
今天开发了一个系统。需要用多一行多列去展示图片。查找了一下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="image/left1.gif" width="165" /></td>
<td width="18" background="image/left.gif" rowspan="2" valign="top"><img src="image/left.gif" width="18" />
</td>
<td valign="top" width="583" bgcolor="#ffffff" rowspan="2">
<table><tr> <td width="560"><img src="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") %>&#13;&#10; <%# 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="image/right.gif"><img src="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>

原文转自:http://www.ltesting.net