分页程序开发流程

第一步,创建一个分页的查询函数

ExpandedBlockStart.gif 代码
     ///   <summary>
    
///  分页查询
    
///   </summary>
    
///   <param name="column"> 要查出的列,可为“*” </param>
    
///   <param name="tableName"> 要查询的表,可联合 </param>
    
///   <param name="where"> 查询条件 </param>
    
///   <param name="orderBy"> 排序规则 </param>
    
///   <param name="pageCountNote"> 每面的记录条数 </param>
    
///   <param name="pageNum"> 要得到第几页 </param>
    
///   <param name="countNote"></param>
    
///   <param name="countPage"></param>
    
///   <returns></returns>
     public   static  DataTable GetDataTable( string  column,  string  tableName,  string   where string  orderBy,  int  pageCountNote,  int  pageNum,  out   int  countNote,  out   int  countPage)
    {
        countNote 
=   0 ;
        countPage 
=   0 ;

        
using  (SqlConnection scon  =   new  SqlConnection(connectionString)) {
            DataTable dt 
=   new  DataTable();
            
try
            {
                
where   =   string .IsNullOrEmpty( where ?   string .Empty :  "  Where  "   +   where ;
                orderBy 
=   string .IsNullOrEmpty(orderBy)  ?   string .Empty :  "  Order By  "   +  orderBy;

                SqlCommand scom 
=   new  SqlCommand();
                scom.Connection 
=  scon;
                scom.CommandText 
=   " Select Count(0) as Num From  "   +  tableName  +   where ;
                scon.Open();
                countNote 
=  ( int )scom.ExecuteScalar();
                
if  (countNote  ==   0 goto  scomclose;

                countPage 
=  countNote  /  pageCountNote;
                countPage 
+=  countNote  %  pageCountNote  ==   0   ?   0  :  1 ;

                
if ( pageNum  <   1 ) pageNum  =   1 ;
                
else   if ( pageNum  >  countPage) pageNum  =  countPage;

                SqlDataAdapter sda 
=   new  SqlDataAdapter(scom);
                scom.CommandText 
=   " Select  "   +  column  +   "  From  "   +  tableName  +   where   +  orderBy;
                sda.Fill(pageCountNote 
*  (pageNum  -   1 ), pageCountNote, dt);

            }
            
catch  (Exception ex)
            {
                
throw  ex;
            }
            
finally  {
                
if  (scon.State  !=  ConnectionState.Closed) scon.Close();
            }

            scomclose:
            
if  (scon.State  !=  ConnectionState.Closed) scon.Close();

            
return  dt;
        }
    }

 

 

第二步,创建一个分页的超连接函数

 

ExpandedBlockStart.gif 代码
public   static   string  BuildPageN( int  PageCount,  int  CurrentPage,  string  strNa)
    {
        System.Text.StringBuilder htmlSb 
=   new  System.Text.StringBuilder();
        htmlSb.Append(
" <span class=\ " splitPage\ " > " );

        
if  (CurrentPage  <=   1 )
            htmlSb.Append(
" <a class=\ " aLose\ " >|<</a><a class=\ " aLose\ " ><<</a> " );
        
else
        {
            htmlSb.AppendFormat(strNa, 
1 " |< " );
            htmlSb.AppendFormat(strNa, CurrentPage 
>   1   ?  CurrentPage  -   1  : CurrentPage,  " << " );
        }

        
int  iBegin  =   1 ;
        
int  iEnd  =  PageCount;
        
if  (CurrentPage  >   5   &&  CurrentPage  <  PageCount  -   5 )
        {
            iBegin 
=  CurrentPage  -   5 ;
            iEnd 
=  CurrentPage  +   5 ;
        }
        
else   if  (CurrentPage  >=  PageCount  -   5 )
            iBegin 
=  PageCount  -   10 ;
        
else   if  (CurrentPage  <=   5 )
            iEnd 
=   11 ;

        
for  ( int  i  =  iBegin; i  <=  iEnd; i ++ )
        {
            
if  (i  ==  CurrentPage)
                htmlSb.Append(
" <a class=\ " aLose\ " > "   +  i  +   " </a> " );
            
else
                htmlSb.AppendFormat(strNa, i, i);

        }
        
if  (CurrentPage  >=  PageCount)
        {
            htmlSb.Append(
" <a class=\ " aLose\ " >>></a><a class=\ " aLose\ " >>|</a> " );
        }
        
else
        {

            htmlSb.AppendFormat(strNa, CurrentPage 
<  PageCount  ?  CurrentPage  +   1  : CurrentPage,  " >> " );
            htmlSb.AppendFormat(strNa, PageCount, 
" >| " );
        }

        htmlSb.Append(
" </span> " );
        
return  htmlSb.ToString();
    }

 

第三步,应用

html

ExpandedBlockStart.gif 代码
     < table width = " 100% "   class = " tableNote " >
    
< tr >< th > 标题 </ th >< th width = " 120 " > 来源 </ th >< th width = " 150 " > 发布日期 </ th ></ tr >
    
< asp:Literal ID = " Literal1 "  runat = " server " ></ asp:Literal >
    
< tr >< td colspan = " 3 " >
    
< asp:Literal ID = " Literal2 "  runat = " server " ></ asp:Literal >
    
</ td ></ tr >
    
</ table >

 

后台C#

ExpandedBlockStart.gif 代码
     protected   void  Page_Load( object  sender, EventArgs e)
    {
        
int  iPage  =   string .IsNullOrEmpty(Request.QueryString[ " Page " ])  ?   1  : Int32.Parse(Request.QueryString[ " Page " ]);

        
int  iCountNote;
        
int  iCountPage;

        DataTable dt 
=  DbHelperSQL.GetDataTable( " title,author,pubDate,link " " tb_rss " null null 20 , iPage,  out  iCountNote,  out  iCountPage);

        System.Text.StringBuilder sbHtml 
=   new  System.Text.StringBuilder();
        
foreach  (DataRow drFor  in  dt.Rows) {
            sbHtml.AppendFormat(
@"
<tr>
<td><a target=""_blank"" href=""{0}"">{1}</a></td>
<td>{2}</td>
<td>{3:yyyy年MM月dd日 HH:mm:ss}</td>
</tr>
" ,
              drFor[
" link " ],
              drFor[
" title " ],
              drFor[
" author " ],
              drFor[
" pubDate " ]
                );
        }
        Literal1.Text 
=  sbHtml.ToString();

        Literal2.Text 
=  DbHelperSQL.BuildPageN(iCountPage, iPage,  " <a href=\ " Default2.aspx ? Page = { 0 }\ " >{1}</a> " );
    }

 也可以研究这个“a id="ctl03_TitleUrl" href="http://www.cnblogs.com/chenxumi/archive/2009/11/05/1596777.html" target="_blank">asp.net简单&高效的分页实现”,但是个人感觉没有我这个简单

转载于:https://www.cnblogs.com/gleamy_ming/archive/2010/01/25/1655565.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值