温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:SpaceBuilder v1.1源代码
当前文件:
SpaceBuider11/BlogControls/Channel/ListPosts.cs,打开代码结构图
SpaceBuider11/BlogControls/Channel/ListPosts.cs,打开代码结构图1//------------------------------------------------------------------------------ 2
// <copyright company="Tunynet"> 3
// Copyright (c) Tunynet Inc. All rights reserved. 4
// </copyright> 5
//------------------------------------------------------------------------------ 6
7
using System; 8
using System.Collections.Generic; 9
using System.Text; 10
using SpaceBuilder.Controls.BaseClasses; 11
using SpaceBuilder.Components; 12
using System.Web.UI.WebControls; 13
using SpaceBuilder.Controls.Utils; 14
using SpaceBuilder.Blogs.Components; 15
using SpaceBuilder.Utils; 16
using SpaceBuilder.Controls; 17
using TunyNet.Utils; 18
using TunyNet.Data.Utils; 19
20
namespace SpaceBuilder.Blogs.Controls 21
{ 22
/// <summary> 23
/// 显示文章列表 24
/// </summary> 25
public class ListPosts : TemplatedWebControl 26
{ 27
SBContext wlContext; 28
29
/// <summary> 30
/// 控件初始化时,父类会调用这个方法 31
/// </summary> 32
/// <param name="e"></param> 33
protected override void OnInit(EventArgs e) 34
{ 35
if (SkinName == null) 36
ExternalSkinFileName = "Blogs/Skin-ListPosts.ascx"; 37
else 38
ExternalSkinFileName = SkinName; 39
40
wlContext = SBContext.Current; 41
42
base.OnInit(e); 43
} 44
45
Child Controls 64
65
/// <summary> 66
/// 加载子控件,父类加载皮肤文件时会调用这个方法 67
/// </summary> 68
protected override void AttachChildControls() 69
{ 70
pageTitle = FindControl("PageTitle") as Literal; 71
title = FindControl("Title") as Literal; 72
73
pager = FindControl("Pager") as Pager; 74
pager2 = FindControl("Pager2") as Pager; 75
76
weblogPostsRepeater = FindControl("WeblogPostsRepeater") as Repeater; 77
weblogPostsRepeater.ItemDataBound += new RepeaterItemEventHandler(WeblogPostsRepeater_ItemDataBound); 78
} 79
80
/// <summary> 81
/// 控件被加载时,会先调用这个方法 82
/// </summary> 83
/// <param name="e"></param> 84
protected override void OnLoad(EventArgs e) 85
{ 86
base.OnLoad(e); 87
EnsureChildControls(); 88
89
/* 90
TagName={0} 91
*/ 92
if (!ValueHelper.IsNullOrEmpty(wlContext.GetStringFromQueryString("TagName", string.Empty))) 93
this.TagName = wlContext.GetStringFromQueryString("TagName", string.Empty); 94
95
if (!Page.IsPostBack) 96
Bind(); 97
} 98
99
/// <summary> 100
/// 绑定数据方法具体实现 101
/// </summary> 102
void Bind() 103
{ 104
if (ValueHelper.IsNullOrEmpty(this.TagName)) 105
return; 106
107
Header.AddTitle(Globals.SiteName + " :: 博客(blog)", Context); 108
109
title.Text=TagName; 110
//封装查询条件 111
BlogThreadQuery query = new BlogThreadQuery(); 112
query.TagName = this.TagName; 113
query.SortBy = BlogThreadSortBy.MostRecent; 114
query.SortOrder = SortOrder.Descending; 115
query.PageIndex = pager.PageIndex; 116
query.IsPublicFilter = true; 117
//从数据库中获取文章列表 118
PagingDataSet<BlogThread> pds = BlogPosts.GetBlogThreads(query); 119
if (pds != null && pds.Records.Count > 0) 120
{ 121
if (pager != null) 122
{ 123
pager.TotalRecords = pds.TotalRecords; 124
pager.PageSize = query.PageSize; 125
} 126
if (pager2 != null) 127
{ 128
pager2.TotalRecords = pds.TotalRecords; 129
pager2.PageSize = query.PageSize; 130
} 131
} 132
133
weblogPostsRepeater.DataSource = pds.Records; 134
weblogPostsRepeater.DataBind(); 135
} 136
137
/// <summary> 138
/// 绑定每条记录项时,会调用这个方法 139
/// 目的是为了给每条记录中对应的每个子控件绑定上数据 140
/// </summary> 141
/// <param name="sender"></param> 142
/// <param name="e"></param> 143
private void WeblogPostsRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) 144
{ 145
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 146
{ 147
BlogThread post = (BlogThread)e.Item.DataItem; 148
if (post != null) 149
{ 150
HyperLink subject = e.Item.FindControl("Subject") as HyperLink; 151
if (subject != null) 152
{ 153
subject.Text = StringUtils.Trim(post.Subject, 20); 154
subject.ToolTip = post.Subject; 155
subject.NavigateUrl = BlogUrls.Instance().ShowPost(post); 156
subject.Attributes["target"] = "_blank"; 157
} 158
//是原创还是转载 159
Literal blogPostType = e.Item.FindControl("BlogPostType") as Literal; 160
if (blogPostType != null) 161
blogPostType.Text = post.BlogPostTypeText; 162
//博客名 163
HyperLink blogName = e.Item.FindControl("BlogName") as HyperLink; 164
if (blogName != null) 165
{ 166
blogName.Text = post.Weblog.SectionName; 167
if (ValueHelper.IsNullOrEmpty(blogName.Text)) 168
blogName.Text = post.User.DisplayName; 169
170
blogName.ToolTip = string.Format("查看 {0} 的博客", blogName.Text); 171
blogName.NavigateUrl = UserUrls.Instance().BlogHome(post.Weblog.ApplicationKey); 172
blogName.Attributes["target"] = "_blank"; 173
} 174
//发表日期 175
Literal postDate = e.Item.FindControl("PostDate") as Literal; 176
if (postDate != null) 177
postDate.Text = Formatter.FormatFriendlyDate(post.PostDate); 178
//评论数 179
Literal commentCount = e.Item.FindControl("CommentCount") as Literal; 180
if (commentCount != null) 181
commentCount.Text = post.ReplyCount.ToString(); 182
//浏览数 183
Literal hitTimes = e.Item.FindControl("HitTimes") as Literal; 184
if (hitTimes != null) 185
hitTimes.Text = post.HitTimes.ToString(); 186
} 187
} 188
} 189
190
191
属性 204
205
} 206
}





}