itemauthor.cs

来自「一个ASP.NET下的中文内容管理和社区系统」· CS 代码 · 共 124 行

CS
124
字号
namespace ASPNET.StarterKit.Communities {
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    


    //*********************************************************************
    //
    // ItemAuthor Class
    //
    // Represents an author displayed in a template
    //
    //*********************************************************************
	[Designer(typeof(ASPNET.StarterKit.Communities.CommunityDesigner))]	
	public class ItemAuthor : WebControl {

        private string _externalImage = "~/communities/common/images/external.gif";

        //*********************************************************************
        //
        // ItemAuthor Constructor
        //
        // Set the default css class
        //
        //*********************************************************************
		public ItemAuthor() : base() {
			CssClass = "itemAuthor";
            EnableViewState = false;
		}


        //*********************************************************************
        //
        // ExternalImage Property
        //
        // The relative path to an image displayed for remote users
        //
        //*********************************************************************
        public string ExternalImage {
            get {return _externalImage;}
            set {_externalImage = value; }
        }


        //*********************************************************************
        //
        // OnDataBinding Method
        //
        // Grab the author from the container's DataItem property
        //
        //*********************************************************************
        override protected void OnDataBinding(EventArgs e) {
            ContentItem item;

            if (NamingContainer is ContentItem)
                item = (ContentItem)NamingContainer;
            else
                item = (ContentItem)NamingContainer.NamingContainer;


            ContentInfo objContentInfo = (ContentInfo)item.DataItem;
            ViewState["Author"] = objContentInfo.Author;
            ViewState["RemoteAuthor"] = objContentInfo.RemoteAuthor;
        }


        //*********************************************************************
        //
        // TagKey Property
        //
        // If local user display a link, otherwise display a span 
        //
        //*********************************************************************

        protected override HtmlTextWriterTag TagKey {
            get {
                if ((string)ViewState["RemoteAuthor"] == String.Empty)
                    return HtmlTextWriterTag.A;
                else
                    return HtmlTextWriterTag.Span;
            }
        }
        

        //*********************************************************************
        //
        // AddAttributesToRender Method
        //
        // Add the HRef that links to the profile page
        //
        //*********************************************************************
        protected override void AddAttributesToRender(HtmlTextWriter writer) {
            if ((string)ViewState["RemoteAuthor"] == String.Empty) {
                string encodedAuthor = String.Format("Users_ShowProfile.aspx?user={0}", Context.Server.UrlEncode((string)ViewState["Author"]));
                writer.AddAttribute(HtmlTextWriterAttribute.Href, encodedAuthor);
                base.AddAttributesToRender(writer);
            }
        }        


        //*********************************************************************
        //
        // RenderContents method
        //
        // Display the link label with author name
        // Notice that we HtmlEncode (Thanks Andrew D!)
        //
        //*********************************************************************
        override protected void RenderContents(HtmlTextWriter writer) {
            if ((string)ViewState["RemoteAuthor"] == String.Empty)
                writer.Write(Context.Server.HtmlEncode((string)ViewState["Author"]));    
            else {
                writer.Write(String.Format("<img src=\"{0}\" />", Page.ResolveUrl(_externalImage)));
                writer.Write(Context.Server.HtmlEncode((string)ViewState["RemoteAuthor"]));    
            }
        }


    }
}

⌨️ 快捷键说明

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