kkding

无心无为 手机电脑一个都不能少。

 

新写的一个商品比较控件CompareGrid,没有做设计视图。有兴趣的联络我。公布源码。

源码下载:https://files.cnblogs.com/kkding/WebControls.rar

未命名.GIF

           
 /*
版权所有:版权所有(C) 2004,Kkding
文件编号:
文件名称:CompareGrid.cs
系统编号:
系统名称:CompareGrid
模块编号:
模块名称:数据条
设计文档:   
完成日期:2005-7-06 17:29
作  者:丁科康
内容摘要:      

文件调用:  
 
*/
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Reflection;
using System.Globalization;
using System.Collections;
using System.Text;
using System.Data;
namespace Kkding.Web.UI.WebControls
{
 /// <summary>
 /// CompareGrid 的摘要说明。
 /// </summary>
    [DefaultProperty("Columns"), ToolboxBitmap(typeof(System.Web.UI.WebControls.DataGrid)),
    ToolboxData("<{0}:CompareGrid runat=server></{0}:CompareGrid>"),Designer(typeof(Kkding.Web.UI.Design.CompareGridDesigner))]
 public class CompareGrid : WebControl
 {
  private CompareGridColumnCollection columnCollection;
        private ArrayList columns = null;
        private ArrayList itemsArray;

        private CompareGridItemCollection itemsCollection;
        private object dataSource;
        private bool isinitcolumn=false;

        [EditorAttribute("System.Web.UI.Design.WebControls.DataGridColumnCollectionEditor", typeof(System.Drawing.Design.UITypeEditor))]
        [DefaultValueAttribute(null)]
        [CategoryAttribute("Default")]
        [DescriptionAttribute("Grid的表头")]
        [PersistenceModeAttribute(PersistenceMode.InnerProperty)]
        [MergablePropertyAttribute(false)]
        public virtual CompareGridColumnCollection Columns
        {
            get
            {
                if (columnCollection == null)
                {
                    columnCollection = new CompareGridColumnCollection(this);
                    if(columns == null)
                    {
                        columns =  new ArrayList(columnCollection);
                    }

                }
                return columnCollection;
            }
        }
        [
        EditorBrowsableAttribute(EditorBrowsableState.Never),
        BindableAttribute(BindableSupport.No),
        BrowsableAttribute(false),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
        ]
        public virtual CompareGridItemCollection Items
        {
            get
            {
                if (itemsCollection == null)
                {
                    if (itemsArray == null)
                    {
                        base.EnsureChildControls();
                    }
                    if (itemsArray == null)
                    {
                        itemsArray = new ArrayList();
                    }
                    itemsCollection = new CompareGridItemCollection(itemsArray);
                }
                return itemsCollection;
            }
        }

        [DefaultValueAttribute(null)]
        [DescriptionAttribute("数据源")]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
        [CategoryAttribute("Data")]
        [BindableAttribute(true)]
        public virtual object DataSource
        {
            get
            {
                return dataSource;
            }

            set
            {
                if (value != null && !(value is IListSource) && !(value is IEnumerable))
                {
                    throw new ArgumentException("Invalid_DataSource_Type");
                }
                dataSource = value;
            }
        }
        [CategoryAttribute("Data")]
        [DescriptionAttribute("Repeater_DataMember")]
        [DefaultValueAttribute("")]
        public virtual string DataMember
        {
            get
            {
                object local = base.ViewState["DataMember"];
                if (local != null)
                {
                    return (String)local;
                }
                else
                {
                    return String.Empty;
                }
            }

            set
            {
                base.ViewState["DataMember"] = value;
            }
        }
        [BindableAttribute(true)]
        [EditorAttribute("System.Web.UI.Design.ImageUrlEditor", typeof(System.Drawing.Design.UITypeEditor))]
        [CategoryAttribute("Appearance")]
        [DescriptionAttribute("Table_BackImageUrl")]
        [DefaultValueAttribute("")]
        public virtual string BackImageUrl
        {
            get
            {
                if (!base.ControlStyleCreated)
                {
                    return String.Empty;
                }
                else
                {
                    return ((TableStyle)base.ControlStyle).BackImageUrl;
                }
            }

            set
            {
                ((TableStyle)base.ControlStyle).BackImageUrl = value;
            }
        }

        [CategoryAttribute("Appearance")]
        [BindableAttribute(true)]
        [DescriptionAttribute("Table_CellSpacing")]
        [DefaultValueAttribute(-1)]
        public virtual int CellSpacing
        {
            get
            {
                if (!base.ControlStyleCreated)
                {
                    return -1;
                }
                else
                {
                    return ((TableStyle)base.ControlStyle).CellSpacing;
                }
            }

            set
            {
                ((TableStyle)base.ControlStyle).CellSpacing = value;
            }
        }

        [DefaultValueAttribute(-1)]
        [CategoryAttribute("Appearance")]
        [BindableAttribute(true)]
        [DescriptionAttribute("Table_CellPadding")]
        public virtual int CellPadding
        {
            get
            {
                if (!base.ControlStyleCreated)
                {
                    return -1;
                }
                else
                {
                    return ((TableStyle)base.ControlStyle).CellPadding;
                }
            }

            set
            {
                ((TableStyle)base.ControlStyle).CellPadding = value;
            }
        }

        [DefaultValueAttribute(GridLines.None)]
        [DescriptionAttribute("Table_GridLines")]
        [CategoryAttribute("Appearance")]
        [BindableAttribute(true)]
        public virtual GridLines GridLines
        {
            get
            {
                if (!base.ControlStyleCreated)
                {
                    return GridLines.None;
                }
                else
                {
                    return ((TableStyle)base.ControlStyle).GridLines;
                }
            }

            set
            {
                ((TableStyle)base.ControlStyle).GridLines = value;
            }
        }

        [BindableAttribute(true)]
        [DescriptionAttribute("Table_HorizontalAlign")]
        [CategoryAttribute("Layout")]
        [DefaultValueAttribute(HorizontalAlign.NotSet)]
        public virtual HorizontalAlign HorizontalAlign
        {
            get
            {
                if (!base.ControlStyleCreated)
                {
                    return HorizontalAlign.NotSet;
                }
                else
                {
                    return ((TableStyle)base.ControlStyle).HorizontalAlign;
                }
            }

            set
            {
                ((TableStyle)base.ControlStyle).HorizontalAlign = value;
            }
        }
        public CompareGrid():base(HtmlTextWriterTag.Table)
        {
           
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            Color color = base.BorderColor;
            if (!color.IsEmpty)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Bordercolor, ColorTranslator.ToHtml(color));
            }
            Unit unit = base.BorderWidth;
            if (GridLines == GridLines.None)
            {
                unit = Unit.Pixel(0);
            }
            else if (unit.IsEmpty || unit.Type != UnitType.Pixel)
            {
                unit = Unit.Pixel(1);
            }
            writer.AddAttribute(HtmlTextWriterAttribute.Border, ((int)unit.Value).ToString(NumberFormatInfo.InvariantInfo));
        }

        protected override Style CreateControlStyle()
        {
            return new TableStyle(base.ViewState);
        }
        internal void OnColumnsChanged()
        {
        }

        protected override void CreateChildControls()
        {
            if(!isinitcolumn)
            {
                InitColumn();
                isinitcolumn=!isinitcolumn;
            }
        }
        protected virtual void InitColumn()
        {
            for(int i=0;i<columnCollection.Count;i++)
            {
                TableCell cell = new TableCell();
                columnCollection[i].InitializeCell(cell , ListItemType.Header);
                columnCollection[i].Cells.AddAt(0,cell);
            }
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {
            if(RunningInDesignMode())
            {
                DesignHtml( writer );
                return ;
            }
            IEnumerator iEnumerator = Columns.GetEnumerator();
           
            while (iEnumerator.MoveNext())
            {
                CompareGridColumn data = ((CompareGridColumn)iEnumerator.Current);
                for(int i=0;i< Items.Count ; i++)
                {
                    data.Cells.Add( (TableCell)Items[i].Controls[0]);
                }
               
                data.RenderControl(writer);
            }
        }


        public override void DataBind()
        {
            OnDataBinding(EventArgs.Empty);
        }
        protected override void OnDataBinding(EventArgs e)
        {
            CreateChildControls();
            base.OnDataBinding(e);
            base.Controls.Clear();
            base.ClearChildViewState();
            CreateControlHierarchy(true);
//            base.ChildControlsCreated = true;
        }
        protected virtual void CreateControlHierarchy(bool useDataSource)
        {
            //IEnumerator iEnumerator = null;
            IEnumerable iEnumerable = null;
            int i = -1;
            if (itemsArray != null)
            {
                itemsArray.Clear();
            }
            else
            {
                itemsArray = new ArrayList();
            }
            if (!useDataSource)
            {
                i = (int)base.ViewState["_!ItemCount"];
                if (i != -1)
                {
                    iEnumerable = new DummyDataSource(i);
                    itemsArray.Capacity = i;
                }
            }
            else
            {
                iEnumerable = DataSourceHelper.GetResolvedDataSource(DataSource, DataMember);
                ICollection iCollection = (ICollection)iEnumerable  ;
                if (iCollection != null)
                {
                    itemsArray.Capacity = iCollection.Count;
                }
            }
            if(iEnumerable!=null)
            {
                IEnumerator iEnumerator = iEnumerable.GetEnumerator();
                try
                {
                    int j=0;
                    while (iEnumerator.MoveNext())
                    {
                        object local = iEnumerator.Current;
                        ListItemType listItemType = (j % 2 != 0) ? ListItemType.AlternatingItem : ListItemType.Item;
                        CompareGridItem repeaterItem = CreateItem(j, listItemType, useDataSource, local);
                        itemsArray.Add(repeaterItem);
                        j++;
                    }
                }
                finally
                {
                    if(iEnumerator is IDisposable)
                    {
                        IDisposable iDisposable = (IDisposable)iEnumerator  ;
                        if (iDisposable != null)
                        {
                            iDisposable.Dispose();
                        }
                    }
                }
               

            }
        }
        private CompareGridItem CreateItem(int itemIndex, ListItemType itemType, bool dataBind, object dataItem)
        {
            CompareGridItem repeaterItem = CreateItem(itemIndex, itemType);
            //RepeaterItemEventArgs repeaterItemEventArgs = new RepeaterItemEventArgs(repeaterItem);
            InitializeItem(repeaterItem);
            if (dataBind)
            {
                repeaterItem.DataItem = dataItem;
                for (int i = 0; i < (int)Columns.Count; i++)
                {
                    TableCell cell = (TableCell)repeaterItem.Controls[i]; //Columns[i].Cells[itemIndex];
                    if(!cell.HasControls())
                    {
                        cell.Text = DataBinder.GetPropertyValue(dataItem,Columns[i].DataField,null);
                    }
                    else
                    {
                        //if(Columns[i] is TemplateColumn)
                        repeaterItem.DataBind();
                    }
                }
            }
            //OnItemCreated(repeaterItemEventArgs);
            //base.Controls.Add(repeaterItem);
            if (dataBind)
            {
                repeaterItem.DataBind();
                //OnItemDataBound(repeaterItemEventArgs);
                repeaterItem.DataItem = null;
            }
            return repeaterItem;
        }
        protected  CompareGridItem CreateItem(int itemIndex, ListItemType itemType)
        {
            return new CompareGridItem(itemIndex, itemType);
        }
        protected virtual void InitializeItem(CompareGridItem item)
        {
            //TableCellCollection tableCellCollection = item.Cells;
            item.Controls.Clear();
            for (int i = 0; i < (int)Columns.Count; i++)
            {
                TableCell tableCell = new TableCell();
                Columns[i].InitializeCell(tableCell,  item.ItemType);
                Columns[i].Cells.Add(tableCell);
                item.Controls.Add(tableCell);
            }
        }
       
        /// <summary>
        /// 是否是设计模式
        /// </summary>
        /// <returns></returns>
        internal bool RunningInDesignMode()
        {
            if (Site != null)
                return Site.DesignMode;
            else
                return false;
        }
        private void DesignHtml( HtmlTextWriter writer)
        {
            StringBuilder designresult = new StringBuilder();
            designresult.Append( "<table>");
            foreach(TableRow row in columns)
            {
                designresult.Append("<tr>");
                foreach(TableCell cell in row.Cells)
                {
                    designresult.Append("<td>");
                    designresult.Append(cell.Text);
                    designresult.Append("</td>");
                }
                designresult.Append("</tr>");
            }
            designresult.Append("</table>");
            writer.WriteLine( designresult.ToString());
        }


       
 }
}

posted on 2005-07-07 15:52  kkding  阅读(3262)  评论(28编辑  收藏  举报

导航