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

📄 displayquota.cs

📁 一个ASP.NET下的中文内容管理和社区系统
💻 CS
字号:
namespace ASPNET.StarterKit.Communities {
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;



    //*********************************************************************
    //
    // DisplayQuota Class
    //
    // Displays quota statistics and validates whether or not user
    // has passed either community or user quota.
    //
    //*********************************************************************
	[Designer(typeof(ASPNET.StarterKit.Communities.CommunityDesigner))]	
    public class DisplayQuota : WebControl, IValidator {


        private bool _isValid = false;
        private string _errorMessage = "You have exceeded your quota. Please contact the Administrator of this Web site.";
        private string _text = "You have exceeded your quota. Please contact the Administrator of this Web site.";
        private bool _displayStatistics = true;
        private QuotaInfo _quotaInfo = null;

        private string _userQuotaText = "User Quota: {0:n0} KB";
        private string _userQuotaUsedText = "User Quota Used: {0:n0} KB";

        private string _communityQuotaText = "Community Quota: {0:n0} KB";
        private string _communityQuotaUsedText = "Community Quota Used: {0:n0} KB";


        private Style _errorStyle = new Style();



    
        //*********************************************************************
        //
        // UserQuotaText Property
        //
        // The text displayed for the user quota.
        //
        //*********************************************************************

        public string UserQuotaText {
            get { return _userQuotaText; }
            set { _userQuotaText = value; }
        }
    
        //*********************************************************************
        //
        // UserQuotaUsedText Property
        //
        // The text displayed for the user quota used.
        //
        //*********************************************************************

        public string UserQuotaUsedText {
            get { return _userQuotaUsedText; }
            set { _userQuotaUsedText = value; }
        }
    
        //*********************************************************************
        //
        // CommunityQuotaText Property
        //
        // The text displayed for the community quota.
        //
        //*********************************************************************

        public string CommunityQuotaText {
            get { return _communityQuotaText; }
            set { _communityQuotaText = value; }
        }
    
        //*********************************************************************
        //
        // CommunityQuotaUsedText Property
        //
        // The text displayed for the community quota used.
        //
        //*********************************************************************

        public string CommunityQuotaUsedText {
            get { return _communityQuotaUsedText; }
            set { _communityQuotaUsedText = value; }
        }



    
        //*********************************************************************
        //
        // ErrorMessage Property
        //
        // The Error Message displayed when quota is passed.
        //
        //*********************************************************************

        public string ErrorMessage {
            get { return _errorMessage; }
            set { _errorMessage = value; }
        }
    
    
        //*********************************************************************
        //
        // ErrorStyle Property
        //
        // The style applied to the error message.
        //
        //*********************************************************************

        public Style ErrorStyle {
            get { return _errorStyle; }
        }
    



    
        //*********************************************************************
        //
        // Text Property
        //
        // The Error Message displayed inline with the control.
        //
        //*********************************************************************

        public string Text {
            get {
                if (_text != String.Empty)
                    return _text;
                else
                    return _errorMessage;
            }
            set {
                _text = value;
            }
        }
            
    
    
        //*********************************************************************
        //
        // DisplayStatistics Property
        //
        // Determines whether control displays quota stats.
        //
        //*********************************************************************

        public bool DisplayStatistics {
            get { return _displayStatistics; }
            set { _displayStatistics = value; }
        }
    

    
        //*********************************************************************
        //
        // IsValid Property
        //
        // Returns true if user passes validation.
        //
        //*********************************************************************
    
        public bool IsValid {
            get {
                return _isValid;
            }
            set {
                _isValid = value;
            }
        }



    
        //*********************************************************************
        //
        // Validate Method
        //
        // Called when validation happens. We check validity in the constructor.
        //
        //*********************************************************************

        public void Validate() {
        }




    
        //*********************************************************************
        //
        // OnInit Method
        //
        // Add this control to the page's validators collection.
        //
        //*********************************************************************

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);
            Page.Validators.Add(this);

            // assign some default styles
            if (_displayStatistics) {
                BorderStyle = BorderStyle.Solid;
                BorderWidth = new Unit("1px");
                BorderColor = System.Drawing.Color.Black;
                BackColor = System.Drawing.Color.WhiteSmoke;
                Font.Size = FontUnit.Parse("12px");
            }
        }



    
        //*********************************************************************
        //
        // OnUnload Method
        //
        // Remove this control from the Validators collection.
        //
        //*********************************************************************
        
        protected override void OnUnload(EventArgs e) {
            if (Page != null) {
                Page.Validators.Remove(this);
            }
            base.OnUnload(e);
        }



    
        //*********************************************************************
        //
        // EvaluateIsValid Method
        //
        // Check whether quota is passed.
        //
        //*********************************************************************

        protected bool EvaluateIsValid() {
            if (_quotaInfo.CommunityQuotaUsed > _quotaInfo.CommunityQuota)
                return false;
                
            if (_quotaInfo.UserQuotaUsed > _quotaInfo.UserQuota)
                return false;
                
            return true;            
        }        


    
        //*********************************************************************
        //
        // Render Method
        //
        // Don't render if no quota info.
        //
        //*********************************************************************

        override protected void Render(HtmlTextWriter writer) {
            if (_quotaInfo != null)
                base.Render(writer);
        }
        


    
        //*********************************************************************
        //
        // RenderContents Method
        //
        // Render quota stats and error message (if appropriate).
        //
        //*********************************************************************

        override protected void RenderContents(HtmlTextWriter writer) {
            // Display Statistics
            if (_displayStatistics) {
                // Display user quota
                RenderUserQuota(writer);
                
                // Display community quota (if administrator)
                // We have to use the Identity object here instead of UserInfo because
                // of the admin menu.
                if (Context.User.IsInRole("Community-Administrators"))
                    RenderCommunityQuota(writer);
            }

            // Display Error Message            
            if (_isValid == false)
                RenderErrorMessage(writer);
            
        }


    
        //*********************************************************************
        //
        // RenderUserQuota Method
        //
        // Render user quota statistics.
        //
        //*********************************************************************

        private void RenderUserQuota(HtmlTextWriter writer) {
            
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
 
            // Display user quota
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(String.Format(_userQuotaText, _quotaInfo.UserQuota/1024));    
            writer.RenderEndTag();


            // Create a spacer cell
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(" ");
            writer.RenderEndTag();
            

            // Display user quota used
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(String.Format(_userQuotaUsedText, _quotaInfo.UserQuotaUsed/1024));    
            writer.RenderEndTag();

        
            writer.RenderEndTag();
      
        
        }



    
        //*********************************************************************
        //
        // RenderCommunityQuota Method
        //
        // Render community quota statistics.
        //
        //*********************************************************************

        private void RenderCommunityQuota(HtmlTextWriter writer) {
            
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
 
            // Display community quota
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(String.Format(_communityQuotaText, _quotaInfo.CommunityQuota/1024));    
            writer.RenderEndTag();


            // Create a spacer cell
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(" ");
            writer.RenderEndTag();

            // Display community quota used
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(String.Format(_communityQuotaUsedText, _quotaInfo.CommunityQuotaUsed/1024));    
            writer.RenderEndTag();

        
            writer.RenderEndTag();
      
        
        }


    
        //*********************************************************************
        //
        // RenderErrorMessage Method
        //
        // Renders an error message.
        //
        //*********************************************************************

        private void RenderErrorMessage(HtmlTextWriter writer) {
            // if display stats, then open TR and TD
            if (_displayStatistics) {
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);   
            }
            
            // write error message
            _errorStyle.AddAttributesToRender(writer);
            writer.RenderBeginTag(HtmlTextWriterTag.Span);            
            writer.Write(_text);
            writer.RenderEndTag();

            // if display stats, close TR and TD
            if (_displayStatistics) {
                writer.RenderEndTag();
                writer.RenderEndTag();   
            }

            
        }



    
        //*********************************************************************
        //
        // TagKey Property
        //
        // If displaying quota stats render a table, otherwise render span.
        //
        //*********************************************************************

        override protected HtmlTextWriterTag TagKey {
            get {
                if (_displayStatistics)
                    return HtmlTextWriterTag.Table;
                else
                    return HtmlTextWriterTag.Span;
            }
        }


    
        //*********************************************************************
        //
        // DisplayQuota Constructor
        //
        // Assign default CSS class and validate quota stats.
        //
        //*********************************************************************

        public DisplayQuota() : base() {
            CssClass="displayQuota";

            // assign a red font to error style by default
            _errorStyle.ForeColor = System.Drawing.Color.Red;
            

            // Get Quota Info
            if (Context != null) {
                if (!Context.User.Identity.IsAuthenticated)
                    throw new Exception("Can't calculate quota without a user");


                _quotaInfo = CommunityGlobals.GetQuotaInfo(Context.User.Identity.Name);
                _isValid = EvaluateIsValid();          
            }
        }
        
 
 
    }
}

⌨️ 快捷键说明

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