为BlogEngine改进分页显示效果

     今天终于有时间再对BlogEngine进行一些修改了,这次修改的是BlogEngine的分页显示,原来的只能显示“上一页”和“下一页”,先看一下改进后的效果图:

木子博客(http://www.moozi.net/)    原创

详细地址:http://www.moozi.net/archive/2008/08/28/To-improve-paging-effect-BlogEngine.aspx

 

下面一起来看一下修改方法:

找到"BlogEngine.Web\User controls\PostList.ascx"
在最后面加上一段代码:

<asp:Literal ID="ltlPages" runat="server"></asp:Literal>
 

打开"BlogEngine.Web\User controls\PostList.ascx.cs"

修改的地方比较零散,大家请看我修改后的代码:

Using

public partial class User_controls_PostList : System.Web.UI.UserControl
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!Page.IsCallback)
        
{
            BindPosts();
            InitPaging();
        }

    }


    
/// <summary>
    
/// Binds the list of posts to individual postview.ascx controls
    
/// from the current theme.
    
/// </summary>

    private void BindPosts()
    
{
        
if (Posts == null || Posts.Count == 0)
        
{
            hlPrev.Visible
= false;
            
return;
        }


        List
<IPublishable> visiblePosts = Posts.FindAll(delegate(IPublishable p) { return p.IsVisible; });

        
int count = Math.Min(BlogSettings.Instance.PostsPerPage, visiblePosts.Count);
        
int page = GetPageIndex();
        
int index = page * count;
        
int stop = count;
        
if (index + count > visiblePosts.Count)
            stop
= visiblePosts.Count - index;

        
if (stop < 0 || stop + index > visiblePosts.Count)
        
{
            hlPrev.Visible
= false;
            hlNext.Visible
= false;
            
return;
        }


        
string query = Request.QueryString["theme"];
        
string theme = !string.IsNullOrEmpty(query) ? query : BlogSettings.Instance.Theme;
        
string path = Utils.RelativeWebRoot + "themes/" + theme + "/PostView.ascx";
        
int counter = 0;

        
foreach (Post post in visiblePosts.GetRange(index, stop))
        
{
            
if (counter == stop)
                
break;

            PostViewBase postView
= (PostViewBase)LoadControl(path);
            postView.ShowExcerpt
= BlogSettings.Instance.ShowDescriptionInPostList;
            postView.Post
= post;
            postView.ID
= post.Id.ToString().Replace("-", string.Empty);
            postView.Location
= ServingLocation.PostList;
            posts.Controls.Add(postView);
            counter
++;
        }


        
if (index + stop == Posts.Count)
            hlPrev.Visible
= false;

        bindPageList(Posts.Count, BlogSettings.Instance.PostsPerPage, page
+1);
    }


    
private void bindPageList(int totalCount, int pageSize, int curPage)
    
{
        
int totalPage;
        
if (totalCount % pageSize != 0)
            totalPage
= totalCount / pageSize + 1;
        
else
            totalPage
= totalCount / pageSize;

        System.Text.StringBuilder sb
= new System.Text.StringBuilder();
        sb.Append(
"<div class=\"pagelist\">"+Environment.NewLine);
        sb.Append(
"  "+GetFirstAndLastClass(1,1,curPage) + Environment.NewLine);

        sb.Append(
"  "+GetPageList(totalPage,curPage)+Environment.NewLine);        

        sb.Append(
"  " + GetFirstAndLastClass(2,totalPage, curPage) + Environment.NewLine);
        sb.Append(
"</div>" + Environment.NewLine);

        ltlPages.Text
= sb.ToString();
    }


    
private string GetPageList(int totalPage, int curPage)
    
{
        System.Text.StringBuilder sb
= new System.Text.StringBuilder();

        
if (totalPage < 8)
        
{
            
for (int i = 1; i <= totalPage; i++)
            
{
                
if (i != curPage)
                    sb.Append(
"  <a href=\"" + GetPagePath() +
                        
"page=" + i + "\">" + i + "</a>" + Environment.NewLine);
                
else
                    sb.Append(
"<span class=\"current\">" + i + "</span>" + Environment.NewLine);
            }

        }

        
else if (curPage <= 4)
        
{
            
for (int i = 1; i <= 5; i++)
            
{
                
if (i != curPage)
                    sb.Append(
"  <a href=\"" + GetPagePath() +
                        
"page=" + i + "\">" + i + "</a>" + Environment.NewLine);
                
else
                    sb.Append(
"<span class=\"current\">" + i + "</span>" + Environment.NewLine);
            }

            sb.Append(
"..." + Environment.NewLine);
            sb.Append(
"  <a href=\"" + GetPagePath() +
                  
"page=" + totalPage + "\">" + totalPage + "</a>" + Environment.NewLine);
        }

        
else if (totalPage - curPage > 3)
        
{
            sb.Append(
"  <a href=\"" + GetPagePath() +
                              
"page=1\">1</a>" + Environment.NewLine);
            sb.Append(
"..." + Environment.NewLine);
            
for (int i = curPage - 1; i <= curPage + 1; i++)
            
{
                
if (i != curPage)
                    sb.Append(
"  <a href=\"" + GetPagePath() +
                        
"page=" + i + "\">" + i + "</a>" + Environment.NewLine);
                
else
                    sb.Append(
"<span class=\"current\">" + i + "</span>" + Environment.NewLine);
            }

            sb.Append(
"..." + Environment.NewLine);
            sb.Append(
"  <a href=\"" + GetPagePath() +
                  
"page=" + totalPage + "\">" + totalPage + "</a>" + Environment.NewLine);
        }

        
else
        
{
            sb.Append(
"  <a href=\"" + GetPagePath() +
                                        
"page=1\">1</a>" + Environment.NewLine);
            sb.Append(
"..." + Environment.NewLine);
            
for (int i = totalPage - 4; i <= totalPage; i++)
            
{
                
if (i != curPage)
                    sb.Append(
"  <a href=\"" + GetPagePath() +
                        
"page=" + i + "\">" + i + "</a>" + Environment.NewLine);
                
else
                    sb.Append(
"<span class=\"current\">" + i + "</span>" + Environment.NewLine);
            }

        }

        
return sb.ToString();
    }


    
private string GetFirstAndLastClass(int p, int page, int curPage)
    
{
        
if (p == 1)
        
{
            
if (page == curPage)
                
return "<span class=\"disabled\">&lt; Prev</span>";
            
else
                
return "<a href=\"" + GetPagePath() + "page=" + (curPage - 1) + "\">&lt; Prev</a>";
        }

        
else if (page == curPage)
        
{
            
return "<span class=\"disabled\">Next &gt;</span>";
        }

        
else
        
{
            
return "<a href=\"" + GetPagePath() + "page=" + (curPage + 1) + "\">Next &gt;</a>";
        }

    }


    
/// <summary>
    
/// Retrieves the current page index based on the QueryString.
    
/// </summary>

    private int GetPageIndex()
    
{
        
int index = 0;
        
if (int.TryParse(Request.QueryString["page"], out index))
            index
--;

        
return index;
    }


    
/// <summary>
    
/// Initializes the Next and Previous links
    
/// </summary>

    private void InitPaging()
    
{
        
int page = GetPageIndex();
        
string url = GetPagePath() + "page={0}";

        
//if (page != 1)
        hlNext.HRef = string.Format(url, page);
        
//else
        
//hlNext.HRef = path.Replace("?", string.Empty);
        
        hlPrev.HRef
= string.Format(url, page + 2);

        
if (page == 0)
            hlNext.Visible
= false;
        
//else
        
//    (Page as BlogBasePage).AddGenericLink("next", "Next page", hlNext.HRef);

        
//if (hlPrev.Visible)
        
//    (Page as BlogBasePage).AddGenericLink("prev", "Previous page", string.Format(url, page + 2));
        hlNext.Visible = false;
        hlPrev.Visible
= false;
    }


    
private string GetPagePath()
    
{
        
string path = Request.RawUrl.Replace("Default.aspx", string.Empty);

        
if (path.Contains("?"))
        
{
            
if (path.Contains("page="))
            
{
                
int index = path.IndexOf("page=");
                path
= path.Substring(0, index);
            }

            
else
            
{
                path
+= "&";
            }

        }

        
else
        
{
            path
+= "?";
        }

        
return path;
    }


    
Properties

}

 

 

最后修改你使用的Theme的CSS文件,加上:

.pagelist
{
    padding-right
: 3px;
    padding-left
: 0px;
    padding-bottom
: 3px;
    margin
: 3px;
    padding-top
: 10px;
    text-align
:left;
}

.pagelist a
{
    border-right
: #eee 1px solid;
    padding-right
: 5px;
    border-top
: #eee 1px solid;
    padding-left
: 5px;
    padding-bottom
: 2px;
    margin
: 2px;
    border-left
: #eee 1px solid;
    color
: #ff5a00;
    padding-top
: 2px;
    border-bottom
: #eee 1px solid;
    text-decoration
: none;
}

.pagelist a:hover
{
    border-right
: #ff5a00 1px solid;
    border-top
: #ff5a00 1px solid;
    border-left
: #ff5a00 1px solid;
    color
: #ff5a00;
    border-bottom
: #ff5a00 1px solid;
}

.pagelist a:active
{
    border-right
: #ff5a00 1px solid;
    border-top
: #ff5a00 1px solid;
    border-left
: #ff5a00 1px solid;
    color
: #ff5a00;
    border-bottom
: #ff5a00 1px solid;
}

.pagelist .current
{
    border-right
: #eee 1px solid;
    padding-right
: 5px;
    border-top
: #eee 1px solid;
    padding-left
: 5px;
    font-weight
: bold;
    padding-bottom
: 2px;
    margin
: 2px;
    border-left
: #eee 1px solid;
    color
: #fff;
    padding-top
: 2px;
    border-bottom
: #eee 1px solid;
    background-color
: #ff5a00;
}

.pagelist .disabled
{
    border-right
: #eee 1px solid;
    padding-right
: 5px;
    border-top
: #eee 1px solid;
    padding-left
: 5px;
    padding-bottom
: 2px;
    margin
: 2px;
    border-left
: #eee 1px solid;
    color
: #ddd;
    padding-top
: 2px;
    border-bottom
: #eee 1px solid;
}
 

修改完成了,可以从这里下载修改后的"PostList.ascx"文件 

PostList.ascx.rar (2.28 kb)

posted @ 2008-08-28 12:49  木子博客  阅读(2056)  评论(4编辑  收藏  举报