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 owner, string viewPath) { this.viewPath = viewPath; this.owner = owner; } public override IHierarchicalEnumerable Select() { DataView dv = (DataView) ((this.owner) as 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 : IHierarchyData, ICustomTypeDescriptor { private DataRowView item; private string dataParentIdField; private string dataIdField; public SqlHierarchyData(string dataParentIdField, string dataIdField, DataRowView 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 crow, ref string path) { foreach (DataRowView row in item.DataView) { string c, d; 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(dataParentIdField, dataIdField, row)); } 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(dataParentIdField, dataIdField, row); } return null; } #region ICustomTypeDescriptor Members System.ComponentModel.AttributeCollection ICustomTypeDescriptor.GetAttributes() { return TypeDescriptor.GetAttributes(this, true); } string ICustomTypeDescriptor.GetClassName() { return TypeDescriptor.GetClassName(this, true); } string ICustomTypeDescriptor.GetComponentName() { return TypeDescriptor.GetComponentName(this, true); } TypeConverter ICustomTypeDescriptor.GetConverter() { return TypeDescriptor.GetConverter(this, true); } EventDescriptor ICustomTypeDescriptor.GetDefaultEvent() { return TypeDescriptor.GetDefaultEvent(this, true); } PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty() { return TypeDescriptor.GetDefaultProperty(this, true); } object ICustomTypeDescriptor.GetEditor(Type editorBaseType) { return TypeDescriptor.GetEditor(this, editorBaseType, true); } EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes) { return TypeDescriptor.GetEvents(this, attributes, true); } EventDescriptorCollection ICustomTypeDescriptor.GetEvents() { return TypeDescriptor.GetEvents(this, true); } PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes) { PropertyDescriptorCollection pds = TypeDescriptor.GetProperties(item); if (pds.Count > 0) { List<SqlHierarchyDataPropertyDescriptor> list = 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(name, null) { 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 o, object 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 : ArrayList, IHierarchicalEnumerable { IHierarchyData IHierarchicalEnumerable.GetHierarchyData(object enumeratedItem) { return (SqlHierarchyData) enumeratedItem; } } }