treeview控件能用的从sql数据源到HierarchicalDataSourceControl数据源转换器

来源:互联网 发布:js中window.open 编辑:程序博客网 时间:2024/05/22 05:27
using System.ComponentModel;using System.Security.Permissions;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;namespace ServerControl1{     [DefaultProperty("DataSourceID")]    [ToolboxData("<{0}:treeds runat=server></{0}:treeds >")]       public class treeds : HierarchicalDataSourceControl     {                          protected override HierarchicalDataSourceView GetHierarchicalView(string viewPath)        {           /* DataView myd = (DataView)(DataSourceID as SqlDataSource).Select(DataSourceSelectArguments.Empty);            //throw new System.NotImplementedException();        SqlHierarchicalEnumerable data =  new SqlHierarchicalEnumerable();;            */         SqlDataSource sdt=   Page.FindControl(DataSourceID.ToString()) as SqlDataSource;            HierarchicalDataSourceView hdm = new SqlHierarchicalDataSourceView(sdt,viewPath);            return hdm;        }         [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]        [Bindable(true)]        [Description("工具工具按钮集设置")]        [Category("工具按钮8-属性设置")]        [IDReferenceProperty(typeof(DataSourceControl))]        [Browsable(true)]        public string DataSourceID        {            get            {                string obj2 = this.ViewState["DataSourceID"as string;                 return (string)obj2;                                          }            set            {                             this.ViewState["DataSourceID"] = value;             //   this.OnDataPropertyChanged();            }        }     //   private object _dataSource;    }}
using System.Data;using System.Web.UI;using System.Web.UI.WebControls; namespace ServerControl1{    public class SqlHierarchicalDataSourceView : HierarchicalDataSourceView    {        private string viewPath;        private IDataSource owner;          public SqlHierarchicalDataSourceView(IDataSource ownerstring viewPath)        {            this.viewPath = viewPath;            this.owner = owner;        }          public override IHierarchicalEnumerable Select()        {            DataView dv = (DataView) ((this.owneras SqlDataSource).Select(DataSourceSelectArguments.Empty);             SqlHierarchicalEnumerable data = new SqlHierarchicalEnumerable();            bool hasParent = false;             foreach (DataRowView crow in dv)            {                object[] array1 = crow.Row.ItemArray;                hasParent = false;                 foreach (DataRowView prow in dv)                {                    object[] array2 = prow.Row.ItemArray;                    //子节点                    string a = crow["pid"].ToString();                    //根节点                   string b = prow["Id"].ToString();                    if (a == b)                    {                        hasParent = true;                        break;                    }                }                //添加根节点                if (!hasParent)                    data.Add(new SqlHierarchyData("pid""Id"crow));            }             return data;        }     }}
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Web;using System.Web.UI; namespace ServerControl1{    public class SqlHierarchyData : IHierarchyDataICustomTypeDescriptor    {        private DataRowView item;        private string dataParentIdField;        private string dataIdField;         public SqlHierarchyData(string dataParentIdFieldstring dataIdFieldDataRowView item)        {            this.item = item;            this.dataParentIdField = dataParentIdField;            this.dataIdField = dataIdField;        }         bool IHierarchyData.HasChildren        {            get            {                foreach (DataRowView row in item.DataView)                {                    object[] array1 = row.Row.ItemArray;                    object[] array2 = item.Row.ItemArray;                    string a = row[dataParentIdField].ToString();                    string b = item[dataIdField].ToString();                   string c = item[dataParentIdField].ToString();                //    HttpContext.Current.Response.Write(array1[2] + " - " + a + " -- " + array2[2] + " - " + b + " <br> ");                     /*if( (int.Parse(a)>10))                        return false;*/                    if (a == b)                    {                        return  true;                    }                }              //  return  true;                return false;            }        }               public Int32 Value        {            get;            set;        }         public Int32 ParentID        {            get;            set;        }         public String Text        {            get;            set;        }        object IHierarchyData.Item        {            get { return item; }        }              public override string ToString()        {            return dataIdField;        }         string IHierarchyData.Path        {            get            {                /*string path = "/*[position()=1]";                 GetPath(item, ref path);                return path;*/                return null;            }        }         private void GetPath(DataRowView crowref string path)        {             foreach (DataRowView row in item.DataView)            {                string cd;                c = crow.Row.ItemArray[2].ToString();                d = crow.Row.ItemArray[0].ToString();                string a = crow[dataParentIdField].ToString();                string b = row[dataIdField].ToString();                if (a == b)                {                    path = "/*[position()=1]" + path;                     //GetPath(row, ref path);                    //HttpContext.Current.Response.Write("begin<br>" + c + "--" + a + "--" + b + "<br>end<br>");                    //HttpContext.Current.Response.Write(path + "<br>");                }                //HttpContext.Current.Response.Write(c + "--" + a + "--" +b + "<br>");             }        }         string IHierarchyData.Type        {            get { return dataIdField; }        }         //获取子节点        IHierarchicalEnumerable IHierarchyData.GetChildren()        {            SqlHierarchicalEnumerable children = new SqlHierarchicalEnumerable();             foreach (DataRowView row in item.DataView)            {                string a = row[dataParentIdField].ToString();                string b = item[dataIdField].ToString();                if (a == b)                    children.Add(new SqlHierarchyData(dataParentIdFielddataIdFieldrow));            }             return children;        }         //获取父节点        IHierarchyData IHierarchyData.GetParent()        {            foreach (DataRowView row in item.DataView)            {                string a = item[dataParentIdField].ToString();                string b = row[dataIdField].ToString();                if (a == b)                    return new SqlHierarchyData(dataParentIdFielddataIdFieldrow);            }             return null;        }         #region ICustomTypeDescriptor Members         System.ComponentModel.AttributeCollection ICustomTypeDescriptor.GetAttributes()        {            return TypeDescriptor.GetAttributes(thistrue);        }         string ICustomTypeDescriptor.GetClassName()        {            return TypeDescriptor.GetClassName(thistrue);        }         string ICustomTypeDescriptor.GetComponentName()        {            return TypeDescriptor.GetComponentName(thistrue);        }         TypeConverter ICustomTypeDescriptor.GetConverter()        {            return TypeDescriptor.GetConverter(thistrue);        }         EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()        {            return TypeDescriptor.GetDefaultEvent(thistrue);        }         PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()        {            return TypeDescriptor.GetDefaultProperty(thistrue);        }         object ICustomTypeDescriptor.GetEditor(Type editorBaseType)        {            return TypeDescriptor.GetEditor(thiseditorBaseTypetrue);        }         EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)        {            return TypeDescriptor.GetEvents(thisattributestrue);        }         EventDescriptorCollection ICustomTypeDescriptor.GetEvents()        {            return TypeDescriptor.GetEvents(thistrue);        }         PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)        {            PropertyDescriptorCollection pds = TypeDescriptor.GetProperties(item);             if (pds.Count > 0)            {                List<SqlHierarchyDataPropertyDescriptorlist = new List<SqlHierarchyDataPropertyDescriptor>();                 foreach (PropertyDescriptor pd in pds)                {                    list.Add(new SqlHierarchyDataPropertyDescriptor(pd.Name));                }                 SqlHierarchyDataPropertyDescriptor[] arr = new SqlHierarchyDataPropertyDescriptor[list.Count];                list.CopyTo(arr);                 return new PropertyDescriptorCollection(arr);            }              return PropertyDescriptorCollection.Empty;         }         PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()        {            return ((ICustomTypeDescriptor)this).GetProperties(null);        }         object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)        {            if (pd is SqlHierarchyDataPropertyDescriptor)            {                return this;            }             return null;        }         #endregion    }     public class SqlHierarchyDataPropertyDescriptor : PropertyDescriptor    {        private string name;          public SqlHierarchyDataPropertyDescriptor(string name)            : base(namenull)        {            this.name = name;        }         public override string Name        {            get { return name; }        }         public override Type ComponentType        {            get { return typeof(SqlHierarchyData); }        }         public override bool IsReadOnly        {            get { return true; }        }         public override Type PropertyType        {            get { return Type.GetType("System.String"); }        }          public override bool CanResetValue(object o)        {            return false;        }         public override object GetValue(object o)        {            SqlHierarchyData shd = o as SqlHierarchyData;             if (shd != null)            {                IHierarchyData hd = (IHierarchyData)shd;                string subject = ((DataRowView)(hd.Item))[name].ToString();                return subject;            }             return null;        }         public override void ResetValue(object o)        {            throw new NotSupportedException();        }          public override void SetValue(object oobject value)        {            throw new NotSupportedException();        }         public override bool ShouldSerializeValue(object o)        {            return true;        }         public override TypeConverter Converter        {            get { return TypeDescriptor.GetConverter(typeof(System.String)); }        }    }}
using System.Collections;using System.Web.UI; namespace ServerControl1{    public class SqlHierarchicalEnumerable : ArrayListIHierarchicalEnumerable    {         IHierarchyData IHierarchicalEnumerable.GetHierarchyData(object enumeratedItem)        {            return (SqlHierarchyDataenumeratedItem;        }    }}

0 0
原创粉丝点击