长文章分页(原创)

首先说一下,分页原理:就是通过插入分隔符,将字符串分成数组然后一个一个的显示出来。
 public static string[] GetMessagePages(string message)
        {
            string[] list = message.Split(new string[] { "{$Page$}" }, StringSplitOptions.RemoveEmptyEntries);
          //以 {$Page$}为分隔符来切割字符串。  
     return list;
        }
第一种输出HTML效果

 public string OutPutHtml(string style,string selectedstyle)//返回输出分页的HTML1
    {
        string url = Request.Url.AbsolutePath;//获取当前URL
        StringBuilder sb = new StringBuilder();
        sb.Append("<table  align='center'valign='middle' cellspacing='4' cellpadding='4' border='1'><tr>");
        sb.Append("<tr>");
        for (int i = 0; i < pagesum; i++)
        {
            if (i == page)
            {
                sb.Append("<td align='center' class='" + selectedstyle + "'><a href='" + String.Format("{0}?id={1}&page={2}", url, Utils.GetQueryInt("id", -1), i + "'>" + (i + 1) + "</a></td>"));
//Utils.GetQueryInt等价Request.QueryString
            }
            else
            {
                sb.Append("<td align='center' class='" + style + "'><a href='" + String.Format("{0}?id={1}&page={2}", url, Utils.GetQueryInt("id", -1), i + "'>" + (i + 1) + "</a></td>"));
             }
                if (i == 19)
                break;
        }
        sb.Append("</tr>");
        sb.Append("</table>");
        return sb.ToString();
    }
第二种输出分页的HTML效果
public string OutPutHtml(string style,string selectedstyle)//返回输出分页的HTML2
    {
        string url = Request.Url.AbsolutePath;
        StringBuilder sb = new StringBuilder();
        sb.Append("<table  align='center'valign='middle' cellspacing='3' cellpadding='3' border='0'><tr>");
        sb.Append("<tr>");
        sb.Append("<td align='center' class='" + style + "'><a href='" + String.Format("{0}?id={1}&page={2}", url, Utils.GetQueryInt("id", -1), (page > 1 ? (page - 1) : 0).ToString()) + "'>上一页</a><td>");
         sb.Append("<td align='center' class='" + style + "'><a href='" + String.Format("{0}?id={1}&page={2}", url, Utils.GetQueryInt("id", -1), (page < pagesum ? (page + 1) : page).ToString()) + "'>下一页</a><td>");
        sb.Append("<td align='center' class='" + style + "'>");
        sb.Append(String.Format("总共:<b>{0}/{1}</b>页", (page + 1), (pagesum + 1)));
        sb.Append("</td>");
        sb.Append("<td align='center' class='" + style + "'>转到:<select onchange='location=this.value;'>");
        for (int i = 0; i < pagesum; i++)
        {
            sb.Append("<option value='" + String.Format("{0}?id={1}&page={2}", url, Utils.GetQueryInt("id",-1),i) + "'");
            if (i == page)
                sb.Append(" selected ");
            sb.Append(">第" + (i+1).ToString() + "页</option>");
        }
        sb.Append("</select></td>");
        sb.Append("</tr>");
        sb.Append("</table>");
        return sb.ToString();
    }


在下面的调用如下:

public int page = Utils.GetQueryInt("page", 0);//通过URL带获取当前页
public int pagesum;总页数

public string Html;
string[] msgpages = Infos.GetMessagePages(“这里是要分布的字符串,一定要以{$Page$}做分隔符”);
        //超过数组索引,置0
        pagesum = msgpages.Length;
        if (page >= msgpages.Length) page = 0;
        this.lblmessage.InnerHtml = Utils.HtmlDecode(msgpages[page]);//显示第几页
 Html = OutPutHtml("put", "selecstye");
在这里我用的put,selecstye样式如下:
.selecstye
{
    background:#296CB3;width:20px;height:20px;
 }
 .selecstye a
 {
  width:100%;height:100%;display:block;
  font-size:12px;
         color:White;
  }
   .selecstye a:hover
 {background:#296CB3;
  }
.put
{
 
 line-height:21px;
 color:#3568B9;
 border:1px solid #9AAFE5;
    text-decoration:none;
 font-size:12px;
 color:Black;width:20px;height:20px;
 }
 .put a
 {width:100%;height:100%;display:block;
  color:Black;
  text-decoration:none;
  }
.put a:hover{background:#296CB3; }

在这里要说明的是,那个全局的Html变量,一定要放在前面页要显示的地方,不然是不会起作用的。。。。

 

posted @ 2008-11-21 09:46  悟〈--觉  阅读(449)  评论(0)    收藏  举报