C# 拼接字符串,自实现翻页功能

 

主要为实现类例这样的功能


    /// <summary>
    /// 返回生成后的页面部分
    /// </summary>
    /// <param name="pagenum">当前页号</param>
    /// <param name="Invitataion">当前贴子号</param>
    public static StringBuilder pagePart(int PageNum,string Invitataion,int PageSize,string platename)
    {
        IForum_Answer access = new IForum_Answer();
        StringBuilder pp = new StringBuilder();
        pp.Append("<table><tr>");
    //回复总个数
        int cc = access.GetAnswerCount(Invitataion);
        double count = access.GetAnswerCount(Invitataion)+0.1;
        int PageNumCount;
    
    // if (Convert.ToInt32(count / PageSize) == 1) 
    // {
    // PageNumCount = 2;
    // }
    ////得到总回复页数
    // else
        PageNumCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(count / PageSize)));
        if (PageNum < 5)
        {
    //1到7或到末// 
            int end=(PageNumCount < 7) ? PageNumCount : 7;
            for (int i = 1; i <= end; i++)
            {
    
                string td = "<td><a href=\"" + Invitataion + "-" + i + ".html\" name=\"locaid\">" + i + "</a></td>";
    
    //string td = "<td><a href=\"http://www.cnblogs.com/StaticPage/ThreePage/"+platename+"/" + Invitataion + "-" + i + ".html\" name=\"locaid\">" + i + "</a></td>";
                pp.Append(td);
            }
    //从1到END
    //当总页号大于7时,页后加...
            if (PageNumCount > 7)
            {
    // pp.Append("<td>...</td><td><a href=\"http://www.cnblogs.com/StaticPage/ThreePage/"+platename+"/" + Invitataion + "-" + PageNumCount + ".html\" name=\"locaid\">" + PageNumCount + "</a></td>"); 
                pp.Append("<td>...</td><td><a href=\"" + Invitataion + "-" + PageNumCount + ".html\" name=\"locaid\">" + PageNumCount + "</a></td>");
    
            }
            pp.Append("</tr></table>");
            return pp;
        }
        else if (PageNum > (PageNumCount - 5 + 1))
        {
            int begin = (PageNumCount > 7) ? (PageNumCount - 7 + 1) : 2;
    //当页前加1...
    //pp.Append("<td><a href=\"http://www.cnblogs.com/StaticPage/ThreePage/" + platename + "/" + Invitataion + "-1.html\" name=\"locaid\">1</a></td><td>...</td>");
            pp.Append("<td><a href=\""+ Invitataion + "-1.html\" name=\"locaid\">1</a></td><td>...</td>");
    
    //for (int i = (PageNumCount - 7 + 1); i <= PageNumCount;i++ )
            for (int i = begin; i <= PageNumCount; i++)
    //for (int i = (PageNumCount - 4 + 1); i <= PageNumCount;i++ )
    
            {
    //string td = "<td><a href=\"http://www.cnblogs.com/StaticPage/ThreePage/" + platename + "/" + Invitataion + "-" + i + ".html\" name=\"locaid\">" + i + "</a></td>";
                string td = "<td><a href=\"" + Invitataion + "-" + i + ".html\" name=\"locaid\">" + i + "</a></td>";
    
                pp.Append(td);
            }
    //从倒数7到末
    //int begin=((PageNumCount-7+1)<)
    //PageNumCount-7+1到PageNumCount
            pp.Append("</tr></table>");
            return pp;
        }
    //在开头和尾之间
        else {
    //pp.Append("<td><a href=\"http://www.cnblogs.com/StaticPage/ThreePage/" + platename + "/" + Invitataion + "-1.html\" name=\"locaid\">1</a></td><td>...</td>");
            pp.Append("<td><a href=\"" + Invitataion + "-1.html\" name=\"locaid\">1</a></td><td>...</td>");
    
            for (int i = PageNum - 3; i <= PageNum + 3; i++)
            {
    //string td = "<td><a href=\"http://www.cnblogs.com/StaticPage/ThreePage/" + platename + "/" + Invitataion + "-" + i + ".html\" name=\"locaid\">" + i + "</a></td>";
                string td = "<td><a href=\"" + Invitataion + "-" + i + ".html\" name=\"locaid\">" + i + "</a></td>";
    
                pp.Append(td);
            }
    //从PageNum-3到PageNum+3
    //pp.Append("<td>...</td><td><a href=\"http://www.cnblogs.com/StaticPage/ThreePage/" + platename + "/" + Invitataion + "-" + PageNumCount + ".html\" name=\"locaid\">" + PageNumCount + "</a></td></tr></table>");
            pp.Append("<td>...</td><td><a href=\"" + Invitataion + "-" + PageNumCount + ".html\" name=\"locaid\">" + PageNumCount + "</a></td></tr></table>");
            return pp;
        }
    }

     

     

    posted @ 2013-05-04 14:15  cclient  阅读(330)  评论(0编辑  收藏  举报