DataList分页

DataList没有内置分页功能,所以我们需要手动来进行分页。
如果用SqlServer则可以用它的存储过程分页,代码如下:
None.gif USE   [ OMS_NET ]
None.gif
GO
ExpandedBlockStart.gifContractedBlock.gif
/**/ /****** 对象:  StoredProcedure [dbo].[mp_DivPageBySql]    脚本日期: 02/12/2007 09:55:13 ******/
None.gif
SET  ANSI_NULLS  ON
None.gif
GO
None.gif
SET  QUOTED_IDENTIFIER  ON
None.gif
GO
None.gif
None.gif
CREATE   procedure   [ dbo ] . [ mp_DivPageBySql ]
None.gif 
@strSql   varchar ( 8000 ), -- sql语句,比如select * from tUser或exec mp_StoreProcedure
None.gif
  @nPageSize   int , -- 表示每页的条数
None.gif
  @nPageCount   int -- 表示返回哪一页
None.gif
as
None.gif    
SET  NOCOUNT  ON  
None.gif    
DECLARE   @P1   INT ,
None.gif    
@nRowCount   INT  
None.gif
None.gif    
-- //注意:@scrollopt = 1 会取得Select的时候的总行数
None.gif
     EXEC  sp_cursoropen  @P1  OUTPUT,  @strSql @scrollopt   =   2 @ccopt   =   335873 @rowcount   =   @nRowCount  OUTPUT
None.gif
None.gif    
IF  ( @P1   !=   0 )
None.gif    
BEGIN
None.gif        
-- SELECT @nRowCount AS nRecordCount, ceiling(1.0 * @nRowCount / @nPageSize) AS nPageCount, @nPageCount AS nPage
None.gif
         SET   @nPageCount   =  ( @nPageCount   -   1 *   @nPageSize   +   1  
None.gif        
EXEC  sp_cursorfetch  @P1 32 @nPageCount @nPageSize   
None.gif        
EXEC  sp_cursorclose  @P1
None.gif    
END

如果用Excel等没有这个存储过程的数据库,我们还可以用下面的方法分页:
首先自己写一个填充Datalist的过程,注意此时不要把获取数据的select放到AccessDataSource里了。
ExpandedBlockStart.gif ContractedBlock.gif      /**/ /// <summary>
InBlock.gif    
/// 普通获取数据
ExpandedBlockEnd.gif    
/// </summary>

None.gif      public   void  BuildGrid()
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        OleDbConnection myCon 
= new OleDbConnection(ConfigurationManager.AppSettings["ConnectionString"]);
InBlock.gif        OleDbDataAdapter myAdapter 
= new OleDbDataAdapter("SELECT * FROM vPingLun order by id", myCon);
InBlock.gif        DataSet ds 
= new DataSet();
InBlock.gif        
int startRecord = (CurrentPage - 1* PageItem;//其中CurrentPage是当前页码,可以用QueryString传入;PageItem是每页的条数
InBlock.gif
        myAdapter.Fill(ds, startRecord, PageItem, "Paging");
InBlock.gif        dataLst.DataSource 
= ds.Tables["Paging"].DefaultView;
InBlock.gif        dataLst.DataBind();
InBlock.gif        myCon.Close();
ExpandedBlockEnd.gif    }

这样只需在PageLoad的时候执行一下BuildGrid();就可以了。下面要做的就是生成页码的html代码:
ContractedBlock.gif ExpandedBlockStart.gif 生成分页 #region 生成分页
InBlock.gif    
protected string MakePage()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int currentPage = Convert.ToInt32(Request.QueryString[0]);//当前页
InBlock.gif
        int iCount = Convert.ToInt32(DB.ExecuteSchaler("select count(id) from PingLun");//总条数
InBlock.gif
        int allPage = iCount / PageItem;//allPage是总的页数,PageItem是每页要显示的条数
InBlock.gif
        if (allPage * PageItem != iCount) allPage += 1;//呵呵,烦琐
InBlock.gif
        string outputStr = "[共"+iCount+"条回复,分"+allPage+"页]&nbsp;&nbsp;";
InBlock.gif        
if (allPage == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            outputStr 
= "<font color=red>共一页</font>";
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for (int i = 1; i < allPage + 1; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (i == currentPage)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    outputStr 
+= "<font color=red>" + i.ToString() + "</font>&nbsp;";
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    outputStr 
+= "<a class='op' href='PopView.aspx?page=" + i.ToString() + "'>" + i.ToString() + "</a>&nbsp;";
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
return outputStr;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif
#endregion

转载于:https://www.cnblogs.com/vagerent/archive/2007/05/18/751258.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值