asp网页,管理产品的后台页,只显示到ID是54的,然后就显示ID是146的商品的,中间的没显示,分页也分了7页,一页20条记录,数据库里也有数据,而且完整,下边是具体代码:
<% sql = "from products where 1=1" page_size = 20 total = conn.Execute("select Count(*) "&sql)(0) If total = 0 Then Response.Write("暂无产品!!") Else page = request.QueryString("page") If page<>"" And IsNumeric(page) Then page = CInt(page) Else page = 1 If total Mod page_size = 0 Then pages = total / page_size Else pages = total \ page_size + 1 If page>pages Then page = pages If page>1 Then Set rs = conn.Execute("select Top "&page_size&" * "&sql&" and id>(select max(id) from (select top "&page_size * (page -1)&" id "&sql&") as maxId)") Else Set rs = conn.Execute("select Top "&page_size&" * "&sql&"") Do While Not rs.EOF %> <tr> <td align="center"><%=rs("id")%></td> <td width="13%" align="center"><img src="../<%=rs("uploadfile")%>" width="50" height="20" title="<%=rs("title")%>" alt="<%=rs("title")%>" /></td> <td width="48%" align="center" style="color:#00F;"><%=rs("title")%></td> <td width="25%" align="center"><input type="button" name="update" value="修改内容" onclick="window.location.href='products_update.asp?id=<%=rs("id")%>' " /> <input type="button" name="delete" value="删除产品" onclick="javascript:if(confirm('确定要删除此类产品吗?删除后不可恢复!')){window.location.href='ManagementProducts.asp?act=del&id=<%=rs("id")%>';}else{history.go(0);}" /></td> </tr> </form> <% rs.movenext Loop rs.Close Set rs = Nothing End If %> </table> <p> </p> <%'以下显示分页 Call PageControl(total, pages, page, "border=0 align=center", "<p align=center>") %>
帮忙看下感激
给楼主一个SQL分页代码吧,我之前在网上找的,感觉很好用。
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create Procedure [dbo].[PageCut] @TableName varchar(50), --表名 @Fields varchar(4000) = '*', --字段名(全部字段为*) @OrderField varchar(4000), --排序字段(必须!支持多字段) @sqlWhere varchar(4000) = Null,--条件语句(不用加where) @pageSize int, --每页多少条记录 @pageIndex int = 1 , --指定当前为第几页 @TotalPage int output --返回总页数 AS Begin Begin Tran --开始事务 Declare @sql nvarchar(4000); Declare @totalRecord int; --计算总记录数 If (isnull(@SqlWhere,'')='') set @sql = 'select @totalRecord = count(*) from ' + @TableName Else set @sql = 'select @totalRecord = count(*) from ' + @TableName + ' where ' + @sqlWhere EXEC sp_executesql @sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--计算总记录数 --计算总页数 Select @TotalPage=CEILING((@totalRecord+0.0)/@PageSize) If (isnull(@SqlWhere,'')='') set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName Else set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName + ' where ' + @SqlWhere --处理页数超出范围情况 If @PageIndex<=0 Set @pageIndex = 1 If @pageIndex>@TotalPage Set @pageIndex = @TotalPage --处理开始点和结束点 Declare @StartRecord int Declare @EndRecord int Set @StartRecord = (@pageIndex-1)*@PageSize + 1 Set @EndRecord = @StartRecord + @pageSize - 1 --select CHARINDEX ('(','123456768') --SELECT SUBSTRING('1234567((68)',0,CHARINDEX ('(','123456768')) IF CHARINDEX('(',@TableName) <>0 SET @TableName=SUBSTRING(@TableName,0,CHARINDEX('(',@TableName)) --继续合成sql语句 Set @Sql = @Sql + ') as ' + @TableName + ' where rowId between ' + Convert(varchar(50),@StartRecord) + ' and ' + Convert(varchar(50),@EndRecord) Exec(@Sql) --------------------------------------------------- If @@Error <> 0 Begin RollBack Tran Return -1 End Else Begin Commit Tran Return @totalRecord ---返回记录总数 End end
SQL加一个排序看下,如果没有排序,数据更新过的吧,会出现问题。
具体在哪加排序呢,刚学不太懂求指导,谢谢了
@觉信:
If page>1 Then Set rs = conn.Execute("select Top "&page_size&" * "&sql&" and id>(select max(id) from (select top "&page_size * (page -1)&" id "&sql &排序 &") as maxId)" &排序) Else Set rs = conn.Execute("select Top "&page_size&" * "&sql&"" &排序)
@Tom.汤: If page>1 Then Set rs = conn.Execute("select Top "&page_size&" * "&sql&" and id>(select max(id) from (select top "&page_size * (page -1)&" id "&sql& order by id&") as maxId)" &order by id) Else Set rs = conn.Execute("select Top "&page_size&" * "&sql&"" &order by id) 这样可以吗,弄好了我再追加分数谢谢了
分页有问题,重新找了个分页的代码,可以了
可以去网上找个分页的存储过程,然后后台代码直接调用就不会有问题