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

📄 contentlist.cs

📁 一个ASP.NET下的中文内容管理和社区系统
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;

namespace ASPNET.StarterKit.Communities {

    //*********************************************************************
    //
    // ContentList Class
    //
    // The ContentList control is used to display all the content items
    // in the Community Starter Kit. The ContentList control has support
    // for paging using the database custom paging mechanism.
    //
    //*********************************************************************

	[Designer(typeof(ASPNET.StarterKit.Communities.CommunityDesigner))]
    public class ContentList : WebControl, INamingContainer, IPostBackEventHandler {

        private ITemplate _itemTemplate = null; 
        private ITemplate _separatorTemplate = null;
        private ITemplate _noContentTemplate = null;
        private ITemplate _headerTemplate = null;
        private ITemplate _footerTemplate = null;
        private ITemplate _alternatingItemTemplate = null;
        private ArrayList _colContentItems = new ArrayList();
        private SectionInfo _sectionInfo;
        private UserInfo _userInfo;
        private int _totalPages;
        private ArrayList _dataSource = null;
        private RepeatLayout _repeatLayout = RepeatLayout.Flow;
        private int _repeatColumns = 1;
        private string _pagerText = String.Empty;
        
        private static readonly object EventItemDataBound = new object();        


        //*********************************************************************
        //
        // AddParsedSubObject Method
        //
        // Ignore everything except for content items between the opening
        // and closing ContentList tags.
        //
        //*********************************************************************
		protected override void AddParsedSubObject(Object obj) 
		{

			if (obj is ContentItem) 
			{
				Controls.Add((ContentItem)obj);
			}
		}

 
        public event EventHandler SelectedIndexChanged;
        public event EventHandler ItemCommand;
 
 
        //*********************************************************************
        //
        // OnSelectedIndexChanged Method
        //
        // This method executes when a user clicks a page number. You can
        // override to do something custom.
        //
        //*********************************************************************

        protected virtual void OnSelectedIndexChanged(EventArgs e) {     
            if (SelectedIndexChanged != null) {
                SelectedIndexChanged(this, e);
            }  
        }


        //*********************************************************************
        //
        // RaisePostBackEvent Method
        //
        // This method is called after the user clicks a page number
        //
        //*********************************************************************
       
        public void RaisePostBackEvent(String eventArgument) {
            if (eventArgument == "next") {
                CurrentPage ++;
                OnSelectedIndexChanged(EventArgs.Empty);
                return;
            }
            
            if (eventArgument == "prev") {
                CurrentPage --;    
                OnSelectedIndexChanged(EventArgs.Empty);
                return;
            }
        
            CurrentPage = Int32.Parse(eventArgument);
            OnSelectedIndexChanged(EventArgs.Empty);
        }    
        

    
  
        //*********************************************************************
        //
        // ContentListItemEventHandler Event
        //
        // We need this event to perform custom actions during databinding
        //
        //*********************************************************************
  
        public event ContentListItemEventHandler ItemDataBound {
            add {
                Events.AddHandler(EventItemDataBound, value);
            }
            remove {
                Events.RemoveHandler(EventItemDataBound, value);
            }
        }



  
        //*********************************************************************
        //
        // OnItemDataBound Method
        //
        // We handle the event raised by this method in the Comments control
        //
        //*********************************************************************
        protected virtual void OnItemDataBound(ContentListItemEventArgs e) {
            ContentListItemEventHandler onItemDataBoundHandler = (ContentListItemEventHandler)Events[EventItemDataBound];
            if (onItemDataBoundHandler != null) 
                onItemDataBoundHandler(this, e);
        }


    
        //*********************************************************************
        //
        // OnItemCommand Method
        //
        // This method is invoked when a child control raises an event.
        // For example, the ItemEditContent control fires this method
        //
        //*********************************************************************

        protected virtual void OnItemCommand(Object s, EventArgs e) {
            if (ItemCommand != null)
                ItemCommand(s, e);
        }


        //*********************************************************************
        //
        // OnBubbleEvent Method
        //
        // This method is executed when a child control bubbles an event
        //
        //*********************************************************************
        protected override bool OnBubbleEvent(object s, EventArgs e) {
            OnItemCommand(s, e);
            return false;
        } 


        //*********************************************************************
        //
        // DataSource Property
        //
        // You can only use ArrayLists as a Data Source
        //
        //*********************************************************************
  
        public ArrayList DataSource {
            get {return _dataSource;}
            set {_dataSource = value; }
        }



        //*********************************************************************
        //
        // RepeatLayout Property
        //
        // Like the DataList control, the ContentList supports both flow
        // and table layouts.
        //
        //*********************************************************************
 
        public RepeatLayout RepeatLayout {
            get {return _repeatLayout; }
            set { _repeatLayout = value; }
        }


        //*********************************************************************
        //
        // RepeatColumns Property
        //
        // When table layout, determines the number of columns to display.
        // This property is used in the PhotoGallery to display photo thumbnails.
        //
        //*********************************************************************
 
        public int RepeatColumns {
            get {return _repeatColumns; }
            set { _repeatColumns = value; }
        }


        //*********************************************************************
        //
        // CurrentPage Property
        //
        // The current page selected in the pager
        //
        //*********************************************************************
 
		public int CurrentPage {
            get {
                if (ViewState["CurrentPage"] == null)
                    return 1;
                else
                    return (int)ViewState["CurrentPage"];
            }
            set {ViewState["CurrentPage"] = value;}       
        }


        //*********************************************************************
        //
        // TotalRecords Property
        //
        // We need the total records to display the proper number of pages
        // in the pager
        //
        //*********************************************************************
 		public int TotalRecords {
            get {
                if (ViewState["TotalRecords"] == null) {
                    return 0;
                }
                return (int)ViewState["TotalRecords"];
            }
            set { ViewState["TotalRecords"] = value; }
        }


        
 
        //*********************************************************************
        //
        // NumItems Property
        //
        // This is the number of content items that we displayed in the
        // page. This property is used internally to track the number of
        // items to display in postback scenarios.
        //
        //*********************************************************************       
        private int NumItems {
            get {
                if (ViewState["NumItems"] == null)
                    return 0;
                else
                    return (int)ViewState["NumItems"];
            }
            set { ViewState["NumItems"] = value; }
        }




        //*********************************************************************
        //
        // PagerText Property
        //
        // The text used to display Page blah of blah
        // A typical value is "Page {0} of {1}"
        //
        //*********************************************************************
		public string PagerText {
            get { return _pagerText; }
            set { _pagerText = value; }
        }



        //*********************************************************************
        //
        // TagKey Property
        //
        // If Flow Layout then use Span, otherwise use Table.
        //
        //*********************************************************************

        override protected HtmlTextWriterTag TagKey {
            get {
                if (_repeatLayout == RepeatLayout.Flow)
                    return HtmlTextWriterTag.Span;
                else
                    return HtmlTextWriterTag.Table;
            }        
        }



        //*********************************************************************
        //
        // ContentList Constructor
        //
        // Grab the SectionInfo and UserInfo objects.
        //
        //*********************************************************************

        public ContentList() {
			if (Context != null) {
				_sectionInfo = (SectionInfo)Context.Items["SectionInfo"];
				_userInfo = (UserInfo)Context.Items["UserInfo"];
			
			}
			else{
				_sectionInfo = new SectionInfo();
			}
		}


        //*********************************************************************
        //
        // DataBind Method
        //
        // When databind is called, create the control hiearchy from the
        // database.
        //
        //*********************************************************************

		override public void DataBind() {
            CreateControlHierarchy(true);        
        
            // Prevent CreateChildControls from executing            
            ChildControlsCreated = true;
        }


        //*********************************************************************
        //
        // CreateChildControls Method
        //
        // When databind is not called, create the control hiearchy from
        // view state.
        //
        //*********************************************************************

        override protected void CreateChildControls() {
            CreateControlHierarchy(false);
		}



        //*********************************************************************
        //
        // CreateControlHiearchy Method
        //
        // This is where all the work is done. Add all the content items
        // into the templates to display the contents of the control.
        //
        //*********************************************************************

        void CreateControlHierarchy(bool useDataSource) {
            int itemCount;
            Object item = null;
            ContentItem ctlItem;

			// don't do anything if no ItemTemplate
			if (_itemTemplate == null)
				return;
           
			// Initialize content items collection
			_colContentItems = new ArrayList();

            // if using data source, then populate arraylist
            if (useDataSource) {
                if (_dataSource == null)
                    return;
   
                // Clear any state
                Controls.Clear();
                ClearChildViewState();

                // retrieve content count
                itemCount = _dataSource.Count;
			} else
                itemCount = NumItems;            
            

            // if no content, then show nocontent template
            if (itemCount == 0 ) {
				if (_noContentTemplate != null) 
				{
					ctlItem = new ContentItem( 0, null, ListItemType.Header);
					NoContentTemplate.InstantiateIn( ctlItem );
					Controls.Add( ctlItem );
					_colContentItems.Add(ctlItem);
				}
            } else {
                // Create header template
                if (_headerTemplate != null) {
                    ctlItem = new ContentItem( 0, null, ListItemType.Header);
                    HeaderTemplate.InstantiateIn( ctlItem );
                    Controls.Add( ctlItem );
                    _colContentItems.Add(ctlItem);
                }
                     
                // loop through content, creating templates
                for (int itemIndex=0;itemIndex<itemCount;itemIndex++) {

                    // if dataSource, then grab item
                    if (useDataSource)
                        item = _dataSource[itemIndex];

                
                    // Add the SeparatorTemplate
                    if (itemIndex > 0 && _separatorTemplate != null) {
                        ctlItem = new ContentItem( itemIndex, null, ListItemType.Separator);
						SeparatorTemplate.InstantiateIn( ctlItem );
						Controls.Add( ctlItem );
                        _colContentItems.Add(ctlItem);
                    }


                    if (itemIndex % 2 != 0 && _alternatingItemTemplate != null) {
                                    
                        // Add the AlternatingItemTemplate
                        ctlItem = new ContentItem( itemIndex, item, ListItemType.AlternatingItem );
                        AlternatingItemTemplate.InstantiateIn( ctlItem );
                        Controls.Add( ctlItem );
                        _colContentItems.Add(ctlItem);
                    } else {
                        // Add the ItemTemplate
                        ctlItem = new ContentItem( itemIndex, item, ListItemType.Item );
						ItemTemplate.InstantiateIn( ctlItem );
						Controls.Add( ctlItem );
                        _colContentItems.Add(ctlItem);
					}

                    // Perform databinding
                    if (useDataSource) {
                        ContentListItemEventArgs e = new ContentListItemEventArgs(ctlItem);
                        ctlItem.DataBind();
                        OnItemDataBound(e);
                    }
                }

⌨️ 快捷键说明

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