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

📄 rating.cs

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

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
	using System.ComponentModel;

    //*********************************************************************
    //
    // Rating Class
    //
    // WebControl that enables users to rate community content. 
    //
    //*********************************************************************

	[Designer(typeof(ASPNET.StarterKit.Communities.CommunityDesigner))]	
    public class Rating : WebControl, INamingContainer {
    
        const string DefaultRatingImagePath = "~/Communities/Common/Images/Ratings/";
    
        string _ratingImagePath = String.Empty;
    
    
        RadioButtonList rdlRating;
        LinkButton btnSubmit;

        UserInfo objUserInfo;
        SectionInfo objSectionInfo;
        ContentInfo objContentInfo;
        
        int _contentPageID;
        int _averageRating = -1;
        int _yourRating = -1;
        
        bool _displayOnly = false;
        string _averageRatingText = "Average Rating: ";
        string _yourRatingText = "Your Rating: ";

        string _goodText = "Good";
        string _badText = "Bad";
        string _submitText = "Submit Rating";
        

        //*********************************************************************
        //
        // RateAction Constructor
        //
        // Retrieves user info object from context.
        //
        //*********************************************************************
        
        public Rating() : base(HtmlTextWriterTag.Table) {
            // Set default CSS class
            CssClass = "rating";
 
			if (Context != null) {           
				// Get UserInfo object
				objUserInfo = (UserInfo)Context.Items["UserInfo"];
	            
				// Get SectionInfo object
				objSectionInfo = (SectionInfo)Context.Items["SectionInfo"];

				// Get ContentInfo
				objContentInfo = (ContentInfo)Context.Items["ContentInfo"];
	            
				_averageRating = objContentInfo.AverageRating;
				_yourRating = objContentInfo.YourRating;
				_contentPageID = objContentInfo.ContentPageID;
			}
        }

        



        //*********************************************************************
        //
        // Rating_Changed Event
        //
        // Event that is raised when a user picks a new rating.
        //
        //*********************************************************************

        public event EventHandler Rating_Changed;


        //*********************************************************************
        //
        // DisplayOnly Property
        //
        // When true, only displays ratings and does not enable user
        // to update ratings. Useful for smaller display footprint.
        //
        //*********************************************************************

        public bool DisplayOnly {
            get { return _displayOnly;}
            set { _displayOnly = value;}
        }


        //*********************************************************************
        //
        // AverageRatingText Property
        //
        // The text that is displayed for Average Rating.
        //
        //*********************************************************************
        
        public string AverageRatingText {
            get {
                if (ViewState["AverageRatingText"] == null)
                    return "Average Rating: ";
                else
                    return (string)ViewState["AverageRatingText"];
            }
            set { ViewState["AverageRatingText"] = value;}
        }
        

        //*********************************************************************
        //
        // YourRatingText Property
        //
        // The text that is displayed for Your Rating.
        //
        //*********************************************************************
                
        public string YourRatingText {
            get { return _yourRatingText; }
            set { _yourRatingText = value; }
        }
        

        //*********************************************************************
        //
        // RatingImagePath Property
        //
        // The path to the directory that contains rating images
        //
        //*********************************************************************
                
        public string RatingImagePath {
            get { return _ratingImagePath; }
            set { _ratingImagePath = value;}
        }



        //*********************************************************************
        //
        // AverageRating Property
        //
        // The Average Rating for this item.
        //
        //*********************************************************************
    
        public int AverageRating {
            get { return _averageRating;}
            set { _averageRating = value; }
        }


        //*********************************************************************
        //
        // YourRating Property
        //
        // The current user's rating.
        //
        //*********************************************************************
            
        public int YourRating {
            get { return _yourRating; }
            set { _yourRating = value; }
        }
    

        //*********************************************************************
        //
        // GoodText Property
        //
        // The text that is displayed for highly rated items.
        //
        //*********************************************************************
        
        public string GoodText {
            get { return _goodText; }
            set { _goodText = value; }
        }


        //*********************************************************************
        //
        // BadText Property
        //
        // The text that is displayed for lowly rated items.
        //
        //*********************************************************************
            
        public string BadText {
            get { return _badText; }
            set { _badText = value; }
        }


        //*********************************************************************
        //
        // SubmitText Property
        //
        // The text that is displayed for submitting a rating.
        //
        //*********************************************************************
            
        public string SubmitText {
            get { return _submitText; }
            set { _submitText = value; }
        }
 

        //*********************************************************************
        //
        // btnSubmit_Click Method
        //
        // This method is executed when a user submits a new rating.
        //
        //*********************************************************************
            
        private void btnSubmit_Click(Object s, EventArgs e) {
            int rating;
            if (rdlRating.SelectedIndex != -1) {
                rating = Int32.Parse(rdlRating.SelectedItem.Value);
                _averageRating = RatingUtility.AddRating(objUserInfo.Username, _contentPageID, rating);        
                _yourRating = rating; 
            }
        }


        //*********************************************************************
        //
        // Rating_Click Method
        //
        // The method raises the Rating_Changed event.
        //
        //*********************************************************************
    
        private void Rating_Clicked(Object sender, EventArgs e) {
                Rating_Changed(this, EventArgs.Empty);
        }

        

        //*********************************************************************
        //
        // CreateChildControls Method
        //
        // If the user can rate content, add the submit button.
        //
        //*********************************************************************
        
        override protected void CreateChildControls() {
            Controls.Clear();
            
            if (objUserInfo.MayRate) {
                // Create RadioButton List
                rdlRating = new RadioButtonList();
                rdlRating.RepeatDirection = RepeatDirection.Horizontal;
                
                for (int i=1;i<6;i++)
                    rdlRating.Items.Add(i.ToString());
                Controls.Add(rdlRating);

                // Create Submit Button
                btnSubmit = new LinkButton();
                btnSubmit.ID = "btnSubmit";
                btnSubmit.Text = SubmitText;
                btnSubmit.Click += new EventHandler(btnSubmit_Click);
                Controls.Add(btnSubmit);    
            }
            
        }        


        //*********************************************************************
        //
        // Controls Property
        //
        // Make sure that when you access a child control, the
        // CreateChildControls method has been called.
        //
        //*********************************************************************
            
        override public ControlCollection Controls {
            get {
                EnsureChildControls();
                return base.Controls;
            }
        }
        



        //*********************************************************************
        //
        // Render Method
        //
        // Don't render if ratings are not enabled.
        //
        //*********************************************************************

        override protected void Render(HtmlTextWriter tw) {
            // Check if ratings are enabled
            if (objSectionInfo.EnableRatings)
                base.Render(tw);
        }
   


        //*********************************************************************
        //
        // RenderContents Method
        //
        // Render the Rating control UI to the browser.
        //
        //*********************************************************************
        
        override protected void RenderContents(HtmlTextWriter tw) {
 
            // Display Checkbox List
            if (objUserInfo.MayRate && YourRating == -1 && !DisplayOnly) {
                    tw.RenderBeginTag(HtmlTextWriterTag.Tr);

                    tw.RenderBeginTag(HtmlTextWriterTag.Td);
                    tw.Write(_badText);
                    tw.RenderEndTag();
            
                    tw.RenderBeginTag(HtmlTextWriterTag.Td);
                    rdlRating.RenderControl(tw);
                    tw.RenderEndTag();
        
                    tw.RenderBeginTag(HtmlTextWriterTag.Td);
                    tw.Write(_goodText);
                    tw.RenderEndTag();
        
                    tw.RenderBeginTag(HtmlTextWriterTag.Td);
                    tw.Write("|");
                    tw.RenderEndTag();

                    tw.RenderBeginTag(HtmlTextWriterTag.Td);
                    btnSubmit.RenderControl(tw);
                    tw.RenderEndTag();
        
                    tw.RenderEndTag(); // close tr

                    // Display Averate Rating
                    // Only when not -1
                    if (_averageRating != -1) {
                        tw.RenderBeginTag(HtmlTextWriterTag.Tr);
    
                        tw.AddAttribute(HtmlTextWriterAttribute.Colspan, "4");
                        tw.RenderBeginTag(HtmlTextWriterTag.Td);
                        tw.Write(_averageRatingText);
                        tw.Write( GetRatingImage(_averageRating) );
                        tw.RenderEndTag();
                        
                        tw.RenderEndTag(); // close tr
                    }
                } else {
                    tw.RenderBeginTag(HtmlTextWriterTag.Tr);

                    // Display Your Rating
                    if (YourRating != -1) {
                        tw.RenderBeginTag(HtmlTextWriterTag.Td);
                        tw.Write(_yourRatingText);
                        tw.Write( GetRatingImage(_yourRating) );
                        tw.RenderEndTag();
                    }
                    
                    if (DisplayOnly) {
                        tw.RenderEndTag(); // close tr
                        tw.RenderBeginTag(HtmlTextWriterTag.Tr);                   
                    }
                    
                    tw.RenderBeginTag(HtmlTextWriterTag.Td);
                    // Display Average Rating
                    if (_averageRating != -1) {
                        tw.Write(_averageRatingText);
                        tw.Write( GetRatingImage(_averageRating) );
                    }
                    else
                        tw.Write("&nbsp;");
                    tw.RenderEndTag();
 
                    tw.RenderEndTag(); // close tr
                 }      
    
 
        }
    


        //*********************************************************************
        //
        // GetRatingImage Method
        //
        // Returns the appropriate image for a particular rating.
        //
        //*********************************************************************
    
        private string GetRatingImage(int rating) {
              
            if (_ratingImagePath != String.Empty)
                return String.Format( "<img src=\"{0}Rating{1}.gif\">", Page.ResolveUrl(_ratingImagePath), rating );
            else
                return String.Format( "<img src=\"{0}Rating{1}.gif\">", Page.ResolveUrl(DefaultRatingImagePath), rating );
        }


    }
}

⌨️ 快捷键说明

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