[功能增强]CommunityServer2.0添加帖子底部带登录的快速回复控件

添加人:iyond五级(2391分)   添加时间:2007-04-11    阅读次数:1257  收藏此教程

    自从公司网站http://bbs.cnfdc.com.cn 用上了cs2.0,就有同事给我提出,能不能添加一个帖子底部的快速回复,这样可以很方便的回复帖子,当时刚接触cs,没有弄,现在,觉得对于cs比较熟悉了,于是就下手做了一个,没想到,成功了,现在就和大家分享一下吧。 

    我采用的是cs的标准方法,即用户控件的方式,建立一个皮肤文件,然后给这个皮肤文件对应写一个类。皮肤文件可以说是skin-Login.ascx和CreateQuickReplay.ascx的混合体,类文件也一样,不过由于要求游客可以通过一并输入用户名和密码的方式来发帖,所以先登录用户,这里就去掉了一些验证,放在用户登录后进行。

    先把初步的皮肤文件的代码放上来,文件名Skin-ThreadQuickReply.ascx,放在default主题目录下

 

<%@ Control Language="C#" %>
<%@ Register TagPrefix="CSD" Namespace="CommunityServer.Discussions.Controls" Assembly="CommunityServer.Discussions" %>
<%@ Register TagPrefix="CS" Namespace="CommunityServer.Controls" Assembly="CommunityServer.Controls" %>
<%@ Import Namespace="CommunityServer.Discussions.Controls" %>
<%@ Import Namespace="CommunityServer.Discussions.Components" %>
<%@ Import Namespace="CommunityServer.Components" %>
<%@ Import Namespace="CommunityServer.Controls" %>
<%@ Import Namespace="CommunityServer" %>
<div class="CommonContentArea">
    
<div class="CommonContent">
        
<div class="CommonFormArea">
        
<ul class="ForumPostList">
        
<li>
                
<div class="ForumPostArea">
                    
<h4 class="ForumPostHeader">
                        
<table cellpadding="0" cellspacing="0" border="0" width="100%">
                            
<tr valign="middle">
                                
<td align="left">快速回复主题</td>
                                
<td align="right" class="ForumPostHeaderControlArea">
                                
</td>
                            
</tr>
                        
</table>
                    
</h4>
                    
<table cellspacing="0" cellpadding="0" border="0" width="100%" style="table-layout: auto;">
                        
<tr valign=top>
                            
<td rowspan="2" class="ForumPostUserArea">
                                
<div class="ForumPostUserContent" style="text-align:left">
                                      
<CSD:UserPermissions runat="server" id="Userpermissions1" />
                                
</div>
                            
</td>
                            
<td class="ForumPostContentArea">
                            
<table width="100%" cellpadding="0" cellspacing="0" border="0">
                            
<tr>
                            
<td class="CommonFormField" colspan="2">
                                    
<asp:Panel ID="UserLoginForm" runat="server">
                                    
<span class="CommonFormFieldName"><CS:ResourceLabel ID="ResourceLabel1" runat="server" ResourceName="Login_UserName" /></span>
                                    
<CS:DefaultButtonTextBox Button = "loginButton" id="username" runat="server" size="30" maxlength="64" />
                                    
<span class="CommonFormFieldName"><CS:ResourceLabel ID="ResourceLabel2" runat="server" ResourceName="Login_Password" /></span>
                                    
<CS:DefaultButtonTextBox Button = "loginButton" TextMode="Password" id="password" runat="server" size="11" maxlength="64" />
                                    
<span class="txt4">(<CSD:ForumAnchor AnchorType="UserForgotPassword" runat="server" />)</span>
                                    
<asp:CheckBox type="checkbox" Checked="true" runat="server" id="autoLogin" />
                                    
<asp:LinkButton id="loginButton" runat="server" CssClass = "CommonTextButton" /><href="/user/CreateUser.aspx">立即注册</a>
                                    
</asp:Panel>

                            
</td>
                            
</tr>
                            
<tr>
                                
<td class="CommonFormFieldName"><CS:ResourceLabel runat="server" ResourceName="SendEmail_Subject" id="rc_send_subject" /></td>
                                
<td class="CommonFormField"><asp:textbox autocomplete="off" id="PostSubject" runat="server" columns="80" /><asp:requiredfieldvalidator id="SubjectValidator" runat="server" Cssclass="CommonValidationWarning" ControlToValidate="PostSubject" >*</asp:requiredfieldvalidator></td>
                            
</tr>
                            
<tr>
                                
<td colspan=2 class="CommonFormField">
                                    
<asp:requiredfieldvalidator id="Requiredfieldvalidator1" runat="server" Cssclass="CommonValidationWarning" ControlToValidate="PostBody" >*</asp:requiredfieldvalidator>
                                    
<CS:Editor runat="Server" EnableHtmlModeEditing="false" id="PostBody" Width="660" Height="200" />
                                
</td>
                            
</tr>
                            
<tr>
                                
<td colspan=2 class="CommonFormField">
                                     
<asp:button id="SavePost" Runat="server" Text = "  发表 " />
                                
</td>
                            
</tr>
                        
</table>
                            
</td>
                        
</tr>
                        
<tr valign="bottom">
                            
<td class="ForumPostFooterArea">                                
                                
<ul class="ForumPostStatistics CommonPrintHidden" style="clear: both;">
                                
<li></li>
                                
</ul>
                            
</td>
                        
</tr>
                    
</table>
                
</div>
                
</li>
</ul>

        
</div>
    
</div>
</div>

然后就是ThreadQuickReply.cs,放在CommunityServerForums20项目的根目录下即可:

 

//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;
using System.Web.Security;

namespace CommunityServer.Discussions.Controls
{
    
/// <summary>
    
/// 用来在主题下直接快速回复
    
/// 作者:王勇
    
/// 日期:2006-8-14
    
/// email:wangyong.yichang@gmail.com
    
/// qq:12586093
    
/// </summary>

    public class ThreadQuickReply : CommunityServer.Controls.TemplatedWebControl
    
{
        
public ThreadQuickReply()
        
{
            Initialize();
        }


        
private void Initialize()
        
{


            
//CSEvents.AuthorizePost();

            
if (context.PostID > 0)
            
{
                postReplyingTo 
= Posts.GetPost(context.PostID, context.User.UserID);
                forum 
= postReplyingTo.Section as Forum;
            }



            
if (postReplyingTo == null)
            
{
                
throw new CSException(CSExceptionType.PostNotFound);
            }


            
// Security check to see if the forum allows anonymous posts
            
//
            
//if (context.User.IsAnonymous && (!forum.EnableAnonymousPosting || !context.SiteSettings.EnableAnonymousUserPosting))
            
//{
            
//    if (!Context.Request.IsAuthenticated)
            
//    {
            
//        Context.Response.Redirect(Globals.GetSiteUrls().Login);
            
//        Context.Response.End();
            
//    }
            
//}

            
if (postReplyingTo.IsLocked)
            
{
                Context.Response.Redirect(Globals.GetSiteUrls().Post(context.PostID));
                Context.Response.End();
            }

        }


        
private Editor postBody;
        
private TextBox postSubject;
        
private IButton button;
        CSContext context 
= CSContext.Current;
        ForumPost postReplyingTo 
= null;
        Forum forum 
= null;

        
//提前登录控件
        TextBox username;
        TextBox password;
        IButton loginButton;
        CheckBox autoLogin;

        
// *********************************************************************
        
//  CreateChildControls
        
//
        /// <summary>
        
/// This event handler adds the children controls.
        
/// </summary>

        // ***********************************************************************/
        protected override void CreateChildControls()
        
{
            
//If the user is already authenticated we have no work to do
            if (Page.Request.IsAuthenticated)
            
{
                
// If the URL is for the login page and the user is already logged in
                
// we need to throw an access denied exception
                
//if (Globals.GetSiteUrls().Login.StartsWith(context.Context.Request.Path))
                
//    throw new CSException(CSExceptionType.UserAlreadyLoggedIn, context.ReturnUrl);

                
//return;
            }

            
base.CreateChildControls();
        }



        
protected override void AttachChildControls()
        
{
            Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            postBody 
= FindControl("PostBody"as Editor;
            postSubject 
= FindControl("PostSubject"as TextBox;
            button 
= FindButton("SavePost");

            
if (button != null)
                button.Click 
+= new EventHandler(button_Click);

            
string replyPrePend = ResourceManager.GetString("CreateEditPost_ReplyPrePend");
            
if (postReplyingTo.Subject.StartsWith(replyPrePend))
            
{
                postSubject.Text 
= Globals.HtmlDecode(postReplyingTo.Subject);
            }

            
else
            
{
                postSubject.Text 
= replyPrePend + Globals.HtmlDecode(postReplyingTo.Subject);
            }


            
//Head.AddTitle(Formatter.RemoveHtml("快速回复: " + postReplyingTo.Subject, 60), Context);

            
//登录部分
            
// Find the username control
            username = (TextBox)FindControl("username");

            
// Find the password control
            password = (TextBox)FindControl("password");

            
// Find the login button
            loginButton = ButtonManager.Create(FindControl("loginButton"));

            
//loginButton.Click += new EventHandler(LoginButton_Click);
            loginButton.Text = ResourceManager.GetString("LoginSmall_Button");

            
//取消登录按钮
            loginButton.Visible = false;

            
//若登录了,则隐藏控件
            Panel UserLoginForm = FindControl("UserLoginForm"as Panel;
            
if (context.User.Username.ToLower() != "anonymous")
            
{
                UserLoginForm.Visible 
= false;
            }



            
// Find the autologin checkbox
            autoLogin = (CheckBox)FindControl("autoLogin");
            autoLogin.Text 
= ResourceManager.GetString("LoginSmall_AutoLogin");
            
//autoLogin.AutoPostBack = true;
            
//autoLogin.CheckedChanged += new EventHandler( AutoLogin_OnCheckedChanged );

            
// Restore autologin status from the auto login cookie
            AutoLoginCookie alCookie = new AutoLoginCookie();
            autoLogin.Checked 
= alCookie.GetValue();

            
//username.Focus();
        }


        
/// <summary>
        
/// Event handler to update auto login cookie value.
        
/// </summary>

        protected void AutoLogin_OnCheckedChanged(Object sender, EventArgs e)
        
{
            AutoLoginCookie alCookie 
= new AutoLoginCookie();
            alCookie.Write(autoLogin.Checked);
        }


        
// *********************************************************************
        
//  LoginButton_Click
        
//
        /// <summary>
        
/// Event handler to handle the login button click event
        
/// </summary>

        // ***********************************************************************/