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

📄 supercontentrotator.cs

📁 asp.net技术内幕的书配源码
💻 CS
字号:
namespace Superexpert {
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Collections;
    using System.Data;



    //*********************************************************************
    //
    // SuperContentRotator Class
    //
    // The SuperContentRotator enables you to randomly display a list of
    // content items from a data source. This control supports templates
    // through its ItemTemplate property.
    //
    // See the SuperContentRotator.aspx file for
    // samples of using this control.
    //
    // This control is from the book ASP.NET Unleashed written by 
    // Stephhen Walther and published by SAMS publishing.
    //
    //*********************************************************************

    public class SuperContentRotator : WebControl, INamingContainer {
        
        private ArrayList _items = new ArrayList();
        private string _dataTextField = String.Empty;
        private string _dataValueField = String.Empty;
        private string _contentFile = String.Empty;
        private object _dataSource;
        private string _dataMember;
        private RepeatLayout _repeatLayout = RepeatLayout.Table;
        private ITemplate _itemTemplate = null;


        private TableStyle _tableStyle = new TableStyle();
        private TableItemStyle _itemStyle = new TableItemStyle();
        private TableItemStyle _alternatingItemStyle = new TableItemStyle();



        //*********************************************************************
        //
        // ItemCount Property
        //
        // Represents the number of items from the data source.
        //
        //*********************************************************************

        public int ItemCount {
            get {
                if (ViewState["ItemCount" ] == null)
                    return 0;
                else
                    return (int)ViewState["ItemCount"];
            }
            
            set { ViewState["ItemCount"] = value; }
        }



        //*********************************************************************
        //
        // RepeatItems Property
        //
        // Represents the number of items to display from the data source.
        //
        //*********************************************************************

        public int RepeatItems {
            get {
                if (ViewState["RepeatItems" ] == null)
                    return 1;
                else
                    return (int)ViewState["RepeatItems"];
            }
            
            set { ViewState["RepeatItems"] = value; }
        }



        //*********************************************************************
        //
        // RepeatLayout Property
        //
        // Represents whether content items are displayed in an HTML table.
        //
        //*********************************************************************

        public RepeatLayout RepeatLayout {
            get { return _repeatLayout; }
            set { _repeatLayout = value; }
        }



        //*********************************************************************
        //
        // DataSource Property
        //
        // The source of the content items. Either this property or 
        // the ContentFile property must be set.
        //
        //*********************************************************************

        public object DataSource {
            get { return _dataSource; }
            set { _dataSource = value; }
        }


        //*********************************************************************
        //
        // DataMember Property
        //
        // The name of the DataTable when a DataSet is supplied for the
        // data source.
        //
        //*********************************************************************

        public string DataMember {
            get { return _dataMember; }
            set { _dataMember = value; }
        }



        //*********************************************************************
        //
        // ContentFile Property
        //
        // The virtual path to an XML file that contains content items.
        //
        //*********************************************************************

        public string ContentFile {
            get { return _contentFile; }
            set { _contentFile = value; }
        }



        //*********************************************************************
        //
        // TableStyle Property
        //
        // Formats the table displayed around the content items.
        //
        //*********************************************************************

        public TableStyle TableStyle {
            get { return _tableStyle; }
        }



        //*********************************************************************
        //
        // ItemStyle Property
        //
        // Formats each content item.
        //
        //*********************************************************************

        public TableItemStyle ItemStyle {
            get { return _itemStyle; }
        } 




        //*********************************************************************
        //
        // AlternatingItemStyle Property
        //
        // Formats every other content item.
        //
        //*********************************************************************

        public TableItemStyle AlternatingItemStyle {
            get { return _alternatingItemStyle; }
        } 




        //*********************************************************************
        //
        // GetDataSource Method
        //
        // Calculate the data source.
        //
        //*********************************************************************

        private IEnumerable GetDataSource(object dataSource, string dataMember) {
            if (dataSource is IEnumerable)
                return (IEnumerable)dataSource;
            
            DataSet dst = dataSource as DataSet; 
            if (dst != null) {
                if (dataMember != String.Empty)
                    return dst.Tables[ DataMember ].DefaultView;
                else
                    return dst.Tables[ 0 ].DefaultView;
            } else
                throw new ArgumentException( "Invalid data source!" );
        }




        //*********************************************************************
        //
        // GetDataSourceFromFile Method
        //
        // Retrieve a DataSet that represents an XML file.
        //
        //*********************************************************************

        private IEnumerable GetDataSourceFromFile(string fileName) {
            DataSet dst = new DataSet();
            dst.ReadXml(Page.MapPath(fileName));
            return dst.Tables[0].DefaultView;
        }

  
        //*********************************************************************
        //
        // ItemTemplate Property
        //
        // Represents the ItemTemplate.
        //
        //*********************************************************************
        [TemplateContainer(typeof(ContentItem))]
        public ITemplate ItemTemplate {
            get {return _itemTemplate; }
            set { _itemTemplate = value; }
        }
  

        //*********************************************************************
        //
        // OnDataBinding Method
        //
        // When databind is called, create the control hiearchy from the
        // datasource.
        //
        //*********************************************************************

		override protected void OnDataBinding(EventArgs e) {
		    // Use an enumerator to creates the items collection
            IEnumerable objData = null;
		    if (_contentFile != String.Empty)
                objData = GetDataSourceFromFile(_contentFile);        
		    else
		        objData = GetDataSource(_dataSource, _dataMember);

            IEnumerator objEnum = objData.GetEnumerator();
		    while (objEnum.MoveNext()) {
		      ContentItem ctlItem = new ContentItem();
		      ctlItem.DataItem = objEnum.Current;  
		      _items.Add(ctlItem);
		    }
		
		    // Add all the controls
            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);
		}



        //*********************************************************************
        //
        // GetRandomContentItem Method
        //
        // Randomly retrieve one content item and remove it so that 
        // the same item is not displayed twice.
        //
        //*********************************************************************
    
        private ContentItem GetRandomContentItem() {
            Random objRan = new Random();
            int contentIndex = objRan.Next( _items.Count );
            ContentItem ctlItem = (ContentItem)_items[ contentIndex ];
            _items.RemoveAt(contentIndex);
            return ctlItem;
        }    


        //*********************************************************************
        //
        // CreateControlHierarchy Method
        //
        // Add all the content items to the ItemTemplate.
        //
        //*********************************************************************
    
        private void CreateControlHierarchy(bool useDataSource) {
            ContentItem ctlItem = null;
            int _displayCount = 0;

            // Check for ItemTemplate
            if (ItemTemplate == null)
                throw new Exception( "You must supply an ItemTemplate!");
    

            // Update Item Count
            if (useDataSource)
                ItemCount = _items.Count;
           
            // Calculate display count
            if (RepeatItems == 0)
                _displayCount = ItemCount;
            else
                _displayCount = RepeatItems;
            if (_displayCount > ItemCount)
                _displayCount = ItemCount;
    
                
            // Create each item
            for (int i=0;i < _displayCount;i++) {
                if (useDataSource)
                    ctlItem = GetRandomContentItem();
                else
                    ctlItem = new ContentItem();
                
                // Assign template specific properties
                ctlItem.ItemIndex = i;
                
                // Add the item to the ItemTemplate    
                ctlItem.ItemType = ListItemType.Item;
                ItemTemplate.InstantiateIn(ctlItem);
                // Add the item to the controls collection
                Controls.Add(ctlItem);
            }                       
  
        }
  
  

        //*********************************************************************
        //
        // RenderContents Method
        //
        // When FlowLayout is table, render an HTML table.
        //
        //*********************************************************************
  
        override protected void RenderContents(HtmlTextWriter writer) {

            // if flow layout mode, use default rendering
            if (_repeatLayout == RepeatLayout.Flow) {
                base.RenderContents(writer);
                return;
            }
            
            // Otherwise, render a table
            _alternatingItemStyle.MergeWith(_itemStyle);

            _tableStyle.AddAttributesToRender(writer);
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            for (int i=0;i < Controls.Count;i++) {
                if (i % 2 == 0)
                    _itemStyle.AddAttributesToRender(writer);
                else
                    _alternatingItemStyle.AddAttributesToRender(writer);
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    Controls[i].RenderControl(writer);
                writer.RenderEndTag();
                writer.RenderEndTag();
            }
            writer.RenderEndTag();
        }  
  
  

    }
    
    

    //*********************************************************************
    //
    // ContentItem Class
    //
    // Represents a particular content item.
    //
    //*********************************************************************
    
    public class ContentItem : Control, INamingContainer {
        private object _dataItem;
        private int _itemIndex;
        private ListItemType _itemType;
        
        public object DataItem {
            get { return _dataItem; }
            set { _dataItem = value; }
        }
        
        public int ItemIndex {
            get { return _itemIndex; }
            set { _itemIndex = value; }
        }
        
        public ListItemType ItemType {
            get { return _itemType; }
            set { _itemType = value; }
        }
         
    
    }
    
    
}

⌨️ 快捷键说明

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