您目前尚未登陆,请选择【登陆】或【注册
首页->博客论坛->SpaceBuilder v1.0正式版源码>>BlogControls/Views/CommentForm.cs>>源码在线查看
温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:SpaceBuilder v1.0正式版源码
当前文件:文件类型 SpaceBuiderV10Source/BlogControls/Views/CommentForm.cs打开代码结构图
普通视图
		            
1//------------------------------------------------------------------------------ 2// <copyright company="Tunynet"> 3// Copyright (c) Tunynet Network Technology Co., Ltd. All rights reserved. 4// </copyright> 5//------------------------------------------------------------------------------ 6 7using System; 8using System.Web; 9using System.Web.UI.HtmlControls; 10using System.Web.UI.WebControls; 11 12using SpaceBuilder.Blogs.Components; 13using SpaceBuilder.Components; 14using SpaceBuilder.Controls.Utils; 15using SpaceBuilder.Security; 16using SpaceBuilder.Controls; 17using TunyNet.Utils; 18using System.Text; 19using SpaceBuilder.Configuration; 20 21namespace SpaceBuilder.Blogs.Controls 22{ 23 /// <summary> 24 /// Summary description for CommentForum. 25 /// </summary> 26 public class CommentForm : WeblogThemedControl 27 { 28 private TextBox subject; 29 private TextBox author; 30 private Editor body; 31 private TextBox url; 32 private CheckBox rememberMe; 33 private LinkButton saveButton; 34 private Label Message; 35 private StatusMessage statusMessage; 36 private Literal maxLengthPerReply; 37 private CustomValidator replayCustomValidator; 38 39 HtmlContainerControl verifyCodeBlock; 40 TextBox verifyCode; 41 Image verifyCodeImage; 42 43 protected override void AttachChildControls() 44 { 45 subject = FindControl("Subject") as TextBox; 46 author = FindControl("Author") as TextBox; 47 body = FindControl("Body") as Editor; 48 url = FindControl("URL") as TextBox; 49 50 verifyCodeBlock = FindControl("VerifyCodeBlock") as HtmlContainerControl; 51 52 if (Context.Request.IsAuthenticated) 53 { 54 author.Text = CurrentUser.DisplayName; 55 url.Text = CurrentUser.Weblog; 56 (FindControl("AuthorDT") as HtmlTableRow).Visible = false; 57 (FindControl("AuthorDT") as HtmlTableRow).EnableViewState = false; 58 (FindControl("AuthorDD") as HtmlTableCell).Visible = false; 59 (FindControl("AuthorDD") as HtmlTableCell).EnableViewState = false; 60 (FindControl("UrlDT") as HtmlTableRow).Visible = false; 61 (FindControl("UrlDT") as HtmlTableRow).EnableViewState = false; 62 (FindControl("UrlDD") as HtmlTableCell).Visible = false; 63 (FindControl("UrlDD") as HtmlTableCell).EnableViewState = false; 64 } 65 else 66 { 67 CheckCookie(); 68 } 69 70 maxLengthPerReply = FindControl("MaxLengthPerReply") as Literal; 71 if (maxLengthPerReply != null) 72 { 73 maxLengthPerReply.Text = string.Format("请您将字数限制在 {0} 以内",SpaceBuilder.Configuration.UserDomainConfiguration.Instance().MaxLengthPerReply); 74 } 75 replayCustomValidator = FindControl("ReplayCustomValidator") as CustomValidator; 76 if (replayCustomValidator != null) 77 { 78 replayCustomValidator.ErrorMessage = string.Format("请您将字数限制在 {0} 以内", SpaceBuilder.Configuration.UserDomainConfiguration.Instance().MaxLengthPerReply); 79 } 80 rememberMe = FindControl("RememberMe") as CheckBox; 81 saveButton = FindControl("SaveButton") as LinkButton; 82 Message = FindControl("Message") as Label; 83 verifyCode = FindControl("VerifyCode") as TextBox; 84 verifyCodeImage = FindControl("VerifyCodeImage") as Image; 85 86 saveButton.Click += new EventHandler(btnSubmit_Click); 87 88 rememberMe.Text = "记住我的信息?"; 89 } 90 91 private BlogThread _dataSource; 92 public BlogThread DataSource 93 { 94 get 95 { 96 if (_dataSource == null) 97 { 98 if (ViewState["PostID"] != null) 99 return BlogPosts.GetThread((int)ViewState["PostID"], false); 100 else 101 return null; 102 } 103 else 104 return this._dataSource; 105 } 106 set 107 { 108 this._dataSource = value; 109 ViewState["PostID"] = value.PostID; 110 } 111 } 112 113 protected override void OnLoad(EventArgs e) 114 { 115 if (this.Visible) 116 { 117 this.EnsureChildControls(); 118 if (this.DataSource == null) 119 { 120 throw new SBException(SBExceptionType.ResourceNotFound, "The blog is not found!"); 121 } 122 if (UseVerifyCode && verifyCodeBlock != null) 123 { 124 verifyCodeBlock.Visible = true; 125 verifyCodeBlock.EnableViewState = false; 126 verifyCodeImage.ImageUrl = GlobalUrls.Instance().VerifyCodeImage(4, 15, true, false); 127 verifyCodeImage.ToolTip = "看不清?点击换一个验证码"; 128 verifyCodeImage.Attributes["onclick"] = "this.src=this.src+'?'"; 129 } 130 131 if (CurrentWeblog.EnableNewComments(CurrentUser)) 132 { 133 subject.Text = "re: " + DataSource.Subject; 134 } 135 else 136 { 137 this.Visible = false; 138 this.EnableViewState = false; 139 return; 140 } 141 } 142 base.OnLoad(e); 143 } 144 145 private void btnSubmit_Click(object sender, EventArgs e) 146 { 147 if (UseVerifyCode && verifyCodeBlock != null) 148 { 149 string verifyCodeFromCookie = Globals.GetVerifyCodeFromCookie(Context); 150 if (!verifyCode.Text.Trim().Equals(verifyCodeFromCookie, StringComparison.CurrentCultureIgnoreCase)) 151 { 152 CustomValidator cv = FindControl("CustomValidatorVerifyCode") as CustomValidator; 153 if (cv != null) 154 cv.IsValid = false; 155 156 return; 157 } 158 } 159 160 if (!Page.IsValid || !CurrentWeblog.EnableNewComments(CurrentUser)) 161 return; 162 if (body.Text.Length > SpaceBuilder.Configuration.UserDomainConfiguration.Instance().MaxLengthPerReply) 163 { 164 replayCustomValidator.IsValid = false; 165 return; 166 } 167 168 BlogPost entry = new BlogPost(); 169 entry.TitleUrl = url.Text; 170 entry.Subject = subject.Text; 171 entry.Body = body.Text; 172 entry.Excerpt = string.Empty; 173 entry.IsApproved = !CurrentWeblog.IsPostModerated(DataSource, CurrentUser); 174 entry.ParentID = DataSource.PostID; 175 entry.ThreadID = DataSource.ThreadID; 176 177 entry.BlogPostType = BlogPostType.Comment; 178 entry.SectionID = DataSource.SectionID; 179 entry.PostDate = DateTime.Now; 180 181 if (Context.Request.IsAuthenticated) 182 { 183 entry.Author = CurrentUser.DisplayName; 184 entry.UserID = CurrentUser.UserID; 185 } 186 else 187 { 188 entry.Author = author.Text; 189 entry.UserID = Users.GetAnonymousUser().UserID; 190 191 // Make sure the URL starts with http:// 192 if (!entry.TitleUrl.StartsWith("http")) 193 entry.TitleUrl = "http://" + entry.TitleUrl; 194 195 // If it is empty, set it to nothing 196 if (entry.TitleUrl == "http://") 197 entry.TitleUrl = string.Empty; 198 199 if (rememberMe.Checked) 200 { 201 HttpCookie userCookie = new HttpCookie("CommentUser"); 202 userCookie["Name"] = System.Web.HttpUtility.UrlEncode(author.Text, System.Text.Encoding.Default); 203 userCookie["Url"] = System.Web.HttpUtility.UrlEncode(url.Text, System.Text.Encoding.Default); 204 Context.Response.SetCookie(userCookie); 205 } 206 } 207 208 entry = BlogPosts.CreatePost(entry); 209 if (entry.PostID > 0) 210 { 211 //Context.Response.Redirect(BlogUrls.Instance().ShowPost(DataSource,CurrentWeblog), true); 212 //Context.Response.Redirect(BlogUrls.Instance().CommentPosted(DataSource, CurrentWeblog)); 213 statusMessage = FindControl("StatusMessage") as StatusMessage; 214 if (statusMessage != null) 215 { 216 statusMessage.MessageType = StatusMessageType.Success; 217 statusMessage.Text = "您的评论已经成功提交,但是因为缓存或者需要审核,可能并不会立即被显示"; 218 statusMessage.Visible = true; 219 statusMessage.EnableRedirect = true; 220 statusMessage.RedirectUrl = BlogUrls.Instance().ShowPost(DataSource, CurrentWeblog); 221 222 saveButton.Enabled = false; 223 } 224 } 225 } 226 227 private void CheckCookie() 228 { 229 if (!Page.IsPostBack) 230 { 231 HttpCookie user = Context.Request.Cookies["CommentUser"]; 232 if (user != null) 233 { 234 author.Text = System.Web.HttpUtility.UrlDecode(user["Name"], Encoding.Default); 235 url.Text = System.Web.HttpUtility.UrlDecode(user["Url"], Encoding.Default); 236 } 237 else 238 { 239 author.Text = "匿名用户"; 240 } 241 } 242 } 243 244 /// <summary> 245 /// 是否使用验证码 246 /// </summary> 247 private bool UseVerifyCode 248 { 249 get { return (!Context.Request.IsAuthenticated && SiteSettingsManager.GetSiteSettings().EnableVerifyCodeToAnonymous); } 250 } 251 252 } 253} 254
还没有找到您心仪的内容?请用.net源码大搜捕
代码片断 打包下载该项目完整源码:SpaceBuilder v1.0正式版源码