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

📄 itemcommentrating.cs

📁 ASP.NET精品全站程序SQL版.rar
💻 CS
字号:
namespace ASPNET.StarterKit.Communities {

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


    //*********************************************************************
    //
    // ItemCommentRating Class
    //
    // WebControl that enables users to rate comments. 
    //
    //*********************************************************************
	[Designer(typeof(ASPNET.StarterKit.Communities.CommunityDesigner))]	
    public class ItemCommentRating : WebControl, INamingContainer {
    
        const string DefaultRatingImagePath = "~/Communities/Common/Images/Ratings/";
    
        string _ratingImagePath = String.Empty;
    
    
        RadioButtonList rdlRating;
        LinkButton btnSubmit;

        UserInfo objUserInfo;
        SectionInfo objSectionInfo;

        
        bool _displayOnly = false;
        string _averageRatingText = "Average Rating: ";
        string _yourRatingText = "Your Rating: ";

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




        //*********************************************************************
        //
        // ItemCommentRating Constructor
        //
        // Retrieves user info object from context.
        //
        //*********************************************************************
        
        public ItemCommentRating() : base(HtmlTextWriterTag.Table) {

            if (Context != null) {
                // Get UserInfo object
                objUserInfo = (UserInfo)Context.Items["UserInfo"];
                // Get SectionInfo object
                objSectionInfo = (SectionInfo)Context.Items["SectionInfo"];
            }
        
            // Set default class
            CssClass = "itemRating";
            EnableViewState = false;
        }


        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;
            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 {
			// rs 2003-09-23
			get { return _averageRatingText; } 
			set { _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;}
        }



        //*********************************************************************
        //
        // ContentPageID Property
        //
        // The Content ID for this item.
        //
        //*********************************************************************
    
        public int ContentPageID {
            get { 
                if (ViewState["ContentPageID"] == null)
                    return -1;
                else
                    return (int)ViewState["ContentPageID"];
            }
            set { ViewState["ContentPageID"] = value; }
        }



        //*********************************************************************
        //
        // AverageRating Property
        //
        // The Average Rating for this item.
        //
        //*********************************************************************
    
        public int AverageRating {
            get { 
                if (ViewState["AverageRating"] == null)
                    return -1;
                else
                    return (int)ViewState["AverageRating"];
            }
            set { ViewState["AverageRating"] = value; }
        }


        //*********************************************************************
        //
        // YourRating Property
        //
        // The current user's rating.
        //
        //*********************************************************************
            
        public int YourRating {
            get {
                if (ViewState["YourRating"] == null)
                    return -1;
                else
                    return (int)ViewState["YourRating"]; 
            }
            set { ViewState["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.EnableCommentRatings)
                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 Current Rating
					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);                   
                    }
                    
                    // Display Average Rating
                    tw.RenderBeginTag(HtmlTextWriterTag.Td);
					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 ratingNumber) {
            if (_ratingImagePath != String.Empty)
                return String.Format( "<img src=\"{0}Rating{1}.gif\">", Page.ResolveUrl(_ratingImagePath), ratingNumber );
            else
                return String.Format( "<img src=\"{0}Rating{1}.gif\">", Page.ResolveUrl(DefaultRatingImagePath), ratingNumber );
        }


    }
}

⌨️ 快捷键说明

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