分页程序开发流程

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

代码
    /// <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 wherestring 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 == 0goto 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;
        }
    }

 

 

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

 

代码
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

代码
    <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#

代码
    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"nullnull20, 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简单&高效的分页实现”,但是个人感觉没有我这个简单

posted @ 2010-01-25 09:06  文明的天空  阅读(298)  评论(0编辑  收藏  举报