📄 postadd.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetFocus.Web.Core;
namespace NetFocus.Web.Applications.Forum
{
public class PostAdd : UserControl, ICallbackEventHandler
{
protected ValuedEditor bodyEditor;
protected ResourceButton saveButton;
protected HiddenField targetControlIDHidden;
protected HiddenField dataWrapperHidden;
private Control targetControl;
private string redirectString = "window.location='" + SiteUrls.Instance().Login + "';";
public string DataWrapperID
{
get
{
string s = ViewState["DataWrapperID"] as string;
if (s == null)
{
s = string.Empty;
}
return s;
}
set
{
ViewState["DataWrapperID"] = value;
}
}
public string TargetControlID
{
get
{
string s = ViewState["TargetControlID"] as string;
if (s == null)
{
s = string.Empty;
}
return s;
}
set
{
ViewState["TargetControlID"] = value;
}
}
protected override void OnLoad(EventArgs e)
{
if (!Page.IsPostBack)
{
this.DataWrapperID = dataWrapperHidden.Value;
this.TargetControlID = targetControlIDHidden.Value;
saveButton.Attributes["onclick"] = string.Format("return addPost();");
RegisterClientScripts();
}
else
{
targetControl = Globals.FindControl(this, TargetControlID);
}
}
#region ICallbackEventHandler Implementation
public void RaiseCallbackEvent(string postContent)
{
if (WebContext.Current.User.IsAnonymous)
{
return;
}
CreatePost(postContent);
((IUpdateData)targetControl).UpdateData();
}
public string GetCallbackResult()
{
string result;
if (WebContext.Current.User.IsAnonymous)
{
result = redirectString;
}
else
{
result = Globals.RenderControl(targetControl);
}
return result;
}
#endregion
private void CreatePost(string postContent)
{
Thread thread = BusinessManager.GetThread(RequestBuilder.BuildThreadOnlyRequest(WebContext.Current.EntityId)) as Thread;
if (thread != null)
{
Post post = new Post();
post.SectionId = thread.SectionId;
post.ParentId = thread.EntityId;
post.Body = postContent;
post.AuthorId = WebContext.Current.User.UserId;
post.Author = WebContext.Current.User.UserName;
post.CreateDate = DateTime.Now;
BusinessManager.CreatePost(post);
thread.TotalPosts++;
thread.MostRecentReplierId = WebContext.Current.User.UserId;
thread.MostRecentReplierName = WebContext.Current.User.UserName;
thread.UpdateDate = DateTime.Now;
Entities.UpdateEntity(thread);
}
}
private void RegisterClientScripts()
{
//Important: 如果是用FCKEditor作为编辑器,则应该用这句代码
//string addPostScript = "function addPost() { var editor = FCKeditorAPI.GetInstance('" + this.bodyEditor.EditorClientID + "'); var content = editor.GetXHTML(true); var strContent = content.toText().Trim(); if(strContent==null||strContent==\"\") { alert('内容不能为空'); return false; } CallServer(content,'" + DataWrapperID + "'); return false; }";
//下面这句代码是针对tinyMCE的
string addPostScript = "function addPost() { var content = tinyMCE.getInstanceById('" + this.bodyEditor.EditorClientID + "').getBody().innerHTML; var strContent = content.toText().Trim(); if(strContent==null||strContent==\"\") { alert('内容不能为空'); return false; } CallServer(content,'" + DataWrapperID + "'); return false; }";
string receiveCallBackScript = "function ReceiveCallback(arg,context){ if (arg == \"" + redirectString + "\") " + redirectString + " else document.getElementById(context).innerHTML = arg; }";
string callbackReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveCallback", "context", false);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), this.ClientID + "AddPostScript", addPostScript, true);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), this.ClientID + "ReceiveCallBackScript", receiveCallBackScript, true);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), this.ClientID + "CallServer", string.Format("function CallServer(arg,context){{ {0} }}", callbackReference), true);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -