⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comments.ascx.cs

📁 BugNET is an issue tracking and project issue management solution built using the ASP.NET web applic
💻 CS
字号:
namespace BugNET.Issues.UserControls
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using BugNET.BusinessLogicLayer;
	using BugNET.UserInterfaceLayer;
	using System.Collections;
    using System.Web.UI;

	/// <summary>
	///		Summary description for Comments.
	/// </summary>
	public partial class Comments : System.Web.UI.UserControl, IIssueTab
	{

		private int _IssueId = 0;
		private int _ProjectId = 0;

        /// <summary>
        /// Gets or sets the bug id.
        /// </summary>
        /// <value>The bug id.</value>
		public int IssueId 
		{
			get { return _IssueId; }
			set { _IssueId = value; }
		}

        /// <summary>
        /// Gets or sets the project id.
        /// </summary>
        /// <value>The project id.</value>
		public int ProjectId 
		{
			get { return _ProjectId; }
			set { _ProjectId = value; }
		}


        /// <summary>
        /// Initializes this instance.
        /// </summary>
		public void Initialize() 
		{
           
			BindComments();

			//check users role permission for adding a comment
            if (!Page.User.Identity.IsAuthenticated || !ITUser.HasPermission(ProjectId, Globals.Permission.ADD_COMMENT.ToString()))
                pnlAddComment.Visible = false;
		}

        /// <summary>
        /// Binds the comments.
        /// </summary>
        private void BindComments()
        {
            IList comments = IssueComment.GetIssueCommentsByIssueId(IssueId);
            if (comments.Count == 0)
            {
                lblComments.Text = GetLocalResourceObject("NoComments").ToString();
                lblComments.Visible = true;
                rptComments.Visible = false;
            }
            else
            {
                lblComments.Visible = false;
                rptComments.DataSource = comments;
                rptComments.DataBind();
                rptComments.Visible = true;
            }


        }

        /// <summary>
        /// Handles the ItemDataBound event of the rptComments control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterItemEventArgs"/> instance containing the event data.</param>
        protected void rptComments_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                IssueComment currentComment = (IssueComment)e.Item.DataItem;

                //if (currentComment.CreatorId == _ReporterUserId)
                //{
                //    ((HtmlControl)e.Item.FindControl("CommentArea")).Attributes["class"] = "CommentAreaOwner";
                //}
                //else
                //{
                //    ((HtmlControl)e.Item.FindControl("CommentArea")).Attributes["class"] = "CommentArea";
                //}             

                // The edit panel is default not shown.
                Panel pnlEditComment = (Panel)e.Item.FindControl("pnlEditComment");
                pnlEditComment.Visible = false;

                Label CreatorDisplayName = (Label)e.Item.FindControl("CreatorDisplayName");
                CreatorDisplayName.Text = ITUser.GetUserDisplayName(currentComment.CreatorUserName);

                Label lblDateCreated = (Label)e.Item.FindControl("lblDateCreated");
                lblDateCreated.Text = currentComment.DateCreated.ToString("f");

                Literal ltlComment = (Literal)e.Item.FindControl("ltlComment");
                ltlComment.Text = Server.HtmlDecode(currentComment.Comment);

                LinkButton lnkDeleteComment = (LinkButton)e.Item.FindControl("lnkDeleteComment");

                lnkDeleteComment.Attributes.Add("onclick", string.Format("return confirm('{0}');", GetLocalResourceObject("DeleteComment").ToString()));


                HyperLink hlPermaLink = (HyperLink)e.Item.FindControl("hlPermalink");
                hlPermaLink.NavigateUrl = String.Format("{0}#{1}", HttpContext.Current.Request.Url, currentComment.Id);

                LinkButton lnkEditComment = (LinkButton)e.Item.FindControl("lnkEditComment");

                // Check if the current user is Authenticated and has permission to edit a comment.
                lnkEditComment.Visible = false;
                if (Page.User.Identity.IsAuthenticated && ITUser.HasPermission(ProjectId, Globals.Permission.EDIT_COMMENT.ToString()))
                {
                    // Check if it is the original user, the project admin or a superuser trying to edit the comment.
                    if (currentComment.CreatorUserName.ToLower() == Context.User.Identity.Name.ToLower() || ITUser.IsInRole(Globals.SuperUserRole) || ITUser.IsInRole(this.ProjectId, Globals.ProjectAdminRole))
                    {
                        lnkEditComment.Visible = true;
                    }
                }

                // Check if the current user is Autheticated and has the permission to delete a comment
                lnkDeleteComment.Visible = false;
                if (Page.User.Identity.IsAuthenticated && ITUser.HasPermission(ProjectId, Globals.Permission.DELETE_COMMENT.ToString()))
                {
                    // Check if it is the original user, the project admin or a superuser trying to delete the comment.
                    if (currentComment.CreatorUserName.ToLower() == Context.User.Identity.Name.ToLower() || ITUser.IsInRole(Globals.SuperUserRole) || ITUser.IsInRole(this.ProjectId, Globals.ProjectAdminRole))
                    {
                        lnkDeleteComment.Visible = true;
                    }
                }

            }
        }

        /// <summary>
        /// Handles the click event of the cmdEditComment
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cmdEditComment_Click(object sender, EventArgs e)
        {
            Control ctrl = (Control)sender;
            Panel pnlEditComment = (Panel)ctrl.Parent;
            BugNET.UserControls.HtmlEditor editor = (BugNET.UserControls.HtmlEditor)pnlEditComment.FindControl("EditCommentHtmlEditor");

            if (editor.Text.Trim().Length != 0)
            {
                HiddenField commentNumber = (HiddenField)pnlEditComment.FindControl("commentNumber");
                int commentID = Convert.ToInt32(commentNumber.Value);

                IssueComment comment = IssueComment.GetIssueCommentById(Convert.ToInt32(commentID));
                comment.Comment = editor.Text.Trim();
                comment.Save();

                editor.Text = String.Empty;
                pnlEditComment.Visible = false;
                commentNumber.Value = String.Empty;

                BindComments();
            }
        }

        //// <summary>
        /// Handles the ItemCommand event of the rptComments control.
        /// </summary>
        /// <param name="source">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterCommandEventArgs"/> instance containing the event data.</param>
        protected void rptComments_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Delete":
                    IssueComment.DeleteIssueCommentById(Convert.ToInt32(e.CommandArgument));
                    BindComments();
                    break;
                case "Edit":
                    IssueComment comment = IssueComment.GetIssueCommentById(Convert.ToInt32(e.CommandArgument));

                    // Show the edit comment panel for the comment
                    Panel pnlEditComment = (Panel)e.Item.FindControl("pnlEditComment");
                    pnlEditComment.Visible = true;

                    // Insert the existing comment text in the edit control.
                    BugNET.UserControls.HtmlEditor editor = (BugNET.UserControls.HtmlEditor)pnlEditComment.FindControl("EditCommentHtmlEditor");                  
                    editor.Text = comment.Comment;

                    // Save the comment ID for further editting.
                    HiddenField commentNumber = (HiddenField)e.Item.FindControl("commentNumber");
                    commentNumber.Value = (string)e.CommandArgument;

                    break;
            }
        }

        /// <summary>
        /// Handles the Click event of the cmdAddComment control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void cmdAddComment_Click(object sender, EventArgs e)
        {
            if (CommentHtmlEditor.Text.Trim().Length != 0)
            {
                IssueComment newComment = new IssueComment(IssueId, CommentHtmlEditor.Text.Trim(), Context.User.Identity.Name);
                newComment.Save();
                CommentHtmlEditor.Text = String.Empty;
                BindComments();
            }
        }
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -